If I were doing it, I would use python and write
fac = 1
for k in xrange(1, 51):
fac *= k
print fac
But you can manufacture the same result in C++ by using an array of ints
each representing a block of digits of the original number. You want to be
able to multiply your number by 50 without overflowing an int. Since 10^9
will fit, so will 50*10^7. So each int in the array will represent 7 digits
of the number.
Now how many will we need? Each of the 49 numbers making up 50! is smaller
than 10^2. Thus 50! is smaller than 10^98. Thus back of the envelope
calculation implies that we can make do with only 14 ints in the array.
int main()
{
int x[14];
for (int j = 1; j < 14; ++j) x[j] = 0;
x[0]=1;
for (int j = 2; j <= 50; ++j)
{
x[0] *= j;
for (int k=1; k < 14; ++k)
{
int q = x[k - 1] / 10000000;
x[k] = x[k] * j + q;
x[k - 1] -= q * 10000000;
}
}
int j = 13;
while (x[j] == 0) --j;
printf("%d", x[j]);
for (int k = j - 1; k >= 0; --k) printf("%07d", x[k]);
printf("\n");
}
Typed straight into gmail using a mobile phone keyboard so Errors and
Omissions Expected.
Luke
On 9 Aug 2011 19:19, "Amahdy" <[email protected]> wrote:
--
You received this message because you are subscribed to the Google Groups
"google-codejam" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-code?hl=en.