Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

A number, as big as 1000! (! = factorial) is given. I need to find how many zeros are there in the number. I counted the no of terminal zeros by dividing 5, 25, 125, ... (untill fifth power < number). But thats just a part of it. I also need to count number of zeros in between the digits. For example 8! has a zero after 4 (40320). I need to count those to.

I spent hours still got nothing :P

Is it actually even possible? I am trying to write a program in C++ to count. I could divide the number by 100 and count zeros but 1000! is too huge even for a 64bit computer.

Any ideas, algorithms, formulae?

share|improve this question

closed as off-topic by Artes, Jens, Michael E2, m_goldberg, Yves Klett Aug 26 at 11:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Jens, m_goldberg, Yves Klett
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This question appears to be off-topic because it is about programming in C++ –  Simon Woods Aug 25 at 16:46
    
Sorry for not being clear.I was trying to get the algorithm in mathematica then try to implement it in C++.I do it everytime.But this time its hard to port it to C++ lol :D –  Yashas Samaga Aug 25 at 16:48
    
    
nope, not the same, I already know to count zeros which are at the right end.But here I want the count the zeros in between too. –  Yashas Samaga Aug 25 at 16:51
2  
You only need to apply DigitCount to the factorial. –  Jens Aug 25 at 18:51

2 Answers 2

up vote 9 down vote accepted
Count[IntegerDigits[#!], 0] & /@ Range[1, 100000, 2000] // ListLogPlot

enter image description here

share|improve this answer
    
Let us note for the record that Count[IntegerDigits[1000!], 0] gives the result 472, essentially instantaneously. –  m_goldberg Aug 25 at 17:05

Counting Strings may be faster, so I propose the string version:

countZeros[x_Integer] := StringCount[IntegerString[x!], "0"]

Then:

countZeros[1000]

472

share|improve this answer
    
Indeed, First@Timing@Count[IntegerDigits[1000!], 0] gives 0.000198 whereas First@Timing@countZeros[1000] gives 0.000157. Moreover, on my system, First@Timing@Count[IntegerDigits[10000!], 0] give 0.003858 whereas First@Timing@countZeros[10000] gives 0.002740. –  murray Aug 25 at 19:06
    
@murray. Thanks for confirming my suspicion. –  RunnyKine Aug 25 at 19:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.