
The last six nonzero digits of 1,000,000! are 412544. Please, keep your money.
I wrote this code to calculate your answer:
using System;
class Program
{
const ulong Target = 1000000; const ulong RoundOff = 10000000000;
static void Main(string[] args)
{
ulong factorial = 1;
for (ulong n = 1; n <= Target; n++)
{
factorial *= n;
while (factorial % 10 == 0)
{
factorial /= 10;
}
factorial %= RoundOff;
}
Console.WriteLine("{0}! (truncated) = {1}", Target, factorial);
}
}
Why it works:
When multiplying numbers, the trailing zeros at the end will not change the outcome... so I threw those out.
Also, if you are only interested in the least significant digits, then the uppermost digits will not have an effect on the outcome of the lower digits... so I threw those out.
With all the rounding, I didn't have to worry about overflow so I was able to calculate the "truncated factorials" of very large numbers iteratively.
Enjoy,
Robert C. Cartaino
modified on Wednesday, July 2, 2008 1:26 PM





Way cool! Thank you very much!
Rod





One thing I forgot to mention in the source code; I rounded off to a few more digits than required because the top digit or two might be wrong because of the rounding. In other words, if you need six digits, calculate for eight. The sixdigit answer I gave above is accurate.





Good Day,
It's me again. This question is one of the questions in ACM ICPC 2006  Philippines.
Basically, you need to express a large number as a product of its Primes.
For example:
120 = 2x2x2x3x5 (or 2^3x3^1x5^1)
I'm lost on how to begin to tackle this problem. Please advice.
Thank you.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Think of how you would do it on paper or using a calculator and generalise...
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."





That is my problem, I don't even know how to do it on paper. I have tried, believe me.
Can you just give me some reading material that will at least help me?
Thanks!
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Ian Uy wrote: I'm lost on how to begin to tackle this problem. Please advice.
As a college student, you really should be able to do this. It's basic math.
What you will want to search for is called "prime factorization." The easiesttounderstand technique is called "trial division."
On paper, you start by writing down your large number (let's call it 'n'). Then try to think of another number which divides evenly into n without any remainder. That number is a factor of n. It's easiest to start with small numbers.
In your example, what number divides evenly into 120 ? How about 2 ? Yes, 120/2=60 . So write down 2 and 60 (those are factors of 120 , but not necessarily prime).
Then try to factor each number you wrote down (the 2 and the 60 ). Are there any numbers (other than 1) that divide into 2 evenly? No, so 2 is a "prime factor" of 120 (i.e. 2 cannot be divided any further).
How about the 60 ? 60/2=30 . So, there is another 2 that is a prime factor of 120 . So far we have the factors of 120=2,2,30 . Now factor the 30 and keep going.
Keep going until you run out of numbers that can be divided evenly. Those will be your prime factors.
120 factored = 2,60 (2 is prime)
60 factored = 2,30 (2 is prime)
30 factored = 2,15 (2 is prime)
15 factored = 3,5 (both 3 and 5 are prime)
Done.
The prime factors of 120 are (from the parentheses above) 2,2,2,3,5 .
Now try writing that in code and see what you come up with. You can use this Table of Prime Factors[^] to check your answers.





Thank you. I have always been really stupid in basic math.
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.





Ian Uy wrote: I have always been really stupid in basic math.
If you are stupid in math i strongly advice you to change you major because math is core for Computer sceince.
The Developer  CEH





Thanks for your reply. I have successfully translated it into C++.
int main()
{
int InputCase;
cin >> InputCase;
for(int i=2;i<=InputCase;i++)
{
if(InputCase%i==0)
{
cout << i;
InputCase/=i;
i;
}
}
return 0;
}
Thanks!
It is said that the most complex structures built by mankind are software systems. This is not generally appreciated because most people cannot see them. Maybe that's a good thing because if we saw them as buildings, we'd deem many of them unsafe.



