Javascript problem with exp. numbers
Go to solution
Solved by Nineshadow,
You don't need to calculate factorial of n to know how many 0-s it has.
It all has to do with combinations of 2 and 5, or powers of 5. It's pretty obvious that you only need to count the number of appearances of 5 (under any form)
In C/C++, the algorithm would be:
long no_0(long n){ long k=0; long x=5; while(x<=n) { k+=n/x; x*=5; } return k;}
Used long because I had to use it in a problem that required that big of a number ( up to 10^9 digits of 0 at the end , or maybe 10^8 , can't recall correctly ; the actual problem was to find n such as n! has p digits of 0 at the end, where p represents the user input; see if you can figure that out
)
It's more efficient and should get rid of the need to work with really large numbers.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now