+ 6

# [CHALLENGE 2] Trailing zeros in factorial

Sample Input 5 Output 1 Because the factorial of 5 is 120 (5*4*3*2*1) so there are 1 zero in it so output is 1 [ UPDATE ] You need to find only trailing zeros means zeros in the ending of number example 1020 output must be 1 Rules : Complexity must be O(n) You can't find factorial of number by loop or recursion Max input 10^6

18 Answers

+ 9

my mathematical explanation ...
//let U have any number ... take it n! , & U want to find maximum value of x such that n!%3^x=0 ,
now how will u find value of x ...
let n! be 100! //just for better explanation , i hv taken that
100 . 99 . 98 . 97 .96 .......1
//we see there are total 33 numbers which are div. by 3
//we see there are total 11 numbers which are div. by 9
//we see there are total 3 numbers which are div. by 27
// & only 1 number which is div by 81
//its obvious to say that max value of x will be 33+11+3+1 =48
//but do u noticed [100/3]=33 , [33/3] =11 , [11/3] is 3 & [3/3] is 1
//[.] denotes g.i.f (greatest integer function)
//similarily u can calculate max y for which n!%2^y =0
//& its obvious that y>x ,
//note that this is only valid for prime & u know the reason ... if not then think
//👉suppose now u want to find max value of k for which , n!%6^k =0
//u know 6 is not prime ... but u can express it in prime ... we need one2 & one 3
//x will be the answer //ie k will be equal to x ☺
👉U can do many modifications in it ... to solve advance level problems also ... hahaha

+ 26

here is my answer for the updated question (trailing zeroes) ☺
https://code.sololearn.com/cWdLm6REukm3/?ref=app

+ 20

here is my try to the problem☺
//no loops used ☺
https://code.sololearn.com/cGPhOX1ROw5A/?ref=app

+ 18

GAME IS OVER
//let me introduce to U my first java-1liner code ☺ , for the first time
//@everyone , my answer don't need any loop.... see pegasus answer its using loop ... while loop
//try my code again ... maths is best ☺☺☺
//works till very big value
here is my answer for the updated question (trailing zeroes) ☺
https://code.sololearn.com/cWdLm6REukm3/?ref=app

+ 14

+ 12

https://code.sololearn.com/cvahzY9m7Q0g/?ref=app

+ 10

My answer to the updated question (trailing zeroes) :
https://code.sololearn.com/cvth23x6QKh0/?ref=app

+ 9

https://code.sololearn.com/cR4UdxXq945Q/?ref=app

+ 8

Here's my code in py:
https://code.sololearn.com/cgj07zHd17G5/?ref=app

+ 6

Lemme give a mathematical answer:
The no. of 5s in the product is always<=no. of 2s.
Hence, the answer is simply:
floor(n/5)+floor(n/25)+floor(n/125)+....
Now I will make an implementation
Edit:
https://code.sololearn.com/cC2TvoTmOZo5/?ref=app
Maths made this possible, works even beyond your limit

+ 6

Challenge accepted
https://code.sololearn.com/c3ce4TL3B0Km/?ref=app

+ 3

+ 3

Here is my try:
https://code.sololearn.com/cT1s0Bbz7Hp5/?ref=app

+ 2

check this one , it's my attempt in java 👍
https://code.sololearn.com/cD3WU69VRmW7/?ref=app

+ 2

My try in c++
https://code.sololearn.com/cg7JNSQwXbWq/?ref=app

+ 2

https://code.sololearn.com/cJdTN8F3Yrq8/?ref=app

0

Here is mine. (JS)
https://code.sololearn.com/WLCGqBqFTLdJ