Skip to content

Commit de3df05

Browse files
TejasVarshneypre-commit-ci[bot]cclauss
authored
Added Trailing Zero Algo (#12104)
* Added Trailing Zero Algo Created an algorithm that return the trailing zeroes of a number * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent bfb5336 commit de3df05

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,7 @@
875875
* [Test Factorial](maths/test_factorial.py)
876876
* [Test Prime Check](maths/test_prime_check.py)
877877
* [Three Sum](maths/three_sum.py)
878+
* [Trailing Zeroes](maths/trailing_zeroes.py)
878879
* [Trapezoidal Rule](maths/trapezoidal_rule.py)
879880
* [Triplet Sum](maths/triplet_sum.py)
880881
* [Twin Prime](maths/twin_prime.py)

maths/trailing_zeroes.py

Lines changed: 40 additions & 0 deletions
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)