Skip to content

Commit 1fe5cad

Browse files
Added Trailing Zero Algo
Created an algorithm that return the trailing zeroes of a number
1 parent 03a4251 commit 1fe5cad

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

maths/trailing_zeroes.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
https://en.wikipedia.org/wiki/Trailing_zero
3+
"""
4+
5+
6+
def trailing_zeroes(num: int) -> int:
7+
"""
8+
Finding the Trailing Zeroes i.e. zeroes present at the end of number
9+
Args:
10+
num: A integer.
11+
Returns:
12+
No. of zeroes in the end of an integer.
13+
14+
>>> trailing_zeroes(1000)
15+
3
16+
>>> trailing_zeroes(102983100000)
17+
5
18+
>>> trailing_zeroes(0)
19+
1
20+
>>> trailing_zeroes(913273)
21+
0
22+
"""
23+
ans = 0
24+
if num < 0:
25+
return -1
26+
if num == 0:
27+
return 1
28+
while num > 0:
29+
if num % 10 == 0:
30+
ans += 1
31+
else:
32+
break
33+
num /= 10
34+
return ans
35+
36+
37+
if __name__ == "__main__":
38+
import doctest
39+
40+
doctest.testmod()

0 commit comments

Comments
 (0)