| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Interesting question, number of 0s at the end of N!
Hello everyone,
My friend and I are discussing about an interesting question, how to calculate efficiently how many 0s are at the end of N! (1 * 2 * 3 * ... * N-1 * N). Our algorithm is like this, 1. Use a loop to calculate N! 2. Use a loop to calculate %10 result of N!, then /10 each step Are there any better solutions? regards, George |
|
#2
|
|||
|
|||
|
I i'm really not a mathematician, but what i can think of:
1. Calculate N! 2. Convert the result to a string, and then count the number of zeroes at the end of the string (really easy to be done). Logical operations are much faster than division, so that would be faster that using %. But the fact remains, that calculating N! is quite time consuming, espec for a litlle larger values of N - try it with wincalc - with values around 80 000 it becomes next to impossible. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Interesting question, number of 0s at the end of N! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|