Skip to content

Commit 935c5db

Browse files
authored
Merge pull request #9 from doingmathwithpython/explorations
Explorations: Miscellaneous code examples
2 parents c6ea910 + c5c13e7 commit 935c5db

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
18.7 KB
Loading
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This program will print the number of trailing zeros the factorial
2+
# of a number
3+
# The formula and the associated explanations are available at:
4+
# https://brilliant.org/wiki/trailing-number-of-zeros/
5+
6+
# Notes on the Python implementation are available on https://doingmathwithpython.github.io
7+
8+
import math
9+
10+
11+
def is_positive_integer(x):
12+
try:
13+
x = float(x)
14+
except ValueError:
15+
return False
16+
else:
17+
if x.is_integer() and x > 0:
18+
return True
19+
else:
20+
return False
21+
22+
23+
def trailing_zeros(num):
24+
if is_positive_integer(num):
25+
# The above function call has done all the sanity checks for us
26+
# so we can just convert this into an integer here
27+
num = int(num)
28+
29+
k = math.floor(math.log(num, 5))
30+
zeros = 0
31+
for i in range(1, k + 1):
32+
zeros = zeros + math.floor(num/math.pow(5, i))
33+
return zeros
34+
else:
35+
print("Factorial of a non-positive non-integer is undefined")
36+
37+
38+
if __name__ == "__main__":
39+
fact_num = input(
40+
"Enter the number whose factorial's trailing zeros you want to find: "
41+
)
42+
num_zeros = trailing_zeros(fact_num)
43+
print("Number of trailing zeros: {0}".format(num_zeros))
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# This program will print the number of trailing zeros the factorial
2+
# of a number
3+
# The formula and the associated explanations are available at:
4+
# https://brilliant.org/wiki/trailing-number-of-zeros/
5+
6+
# Notes on the Python implementation are available on https://doingmathwithpython.github.io
7+
8+
import math
9+
import matplotlib.pyplot as plt
10+
11+
12+
def is_positive_integer(x):
13+
try:
14+
x = float(x)
15+
except ValueError:
16+
return False
17+
else:
18+
if x.is_integer() and x > 0:
19+
return True
20+
else:
21+
return False
22+
23+
24+
def trailing_zeros(num):
25+
if is_positive_integer(num):
26+
# The above function call has done all the sanity checks for us
27+
# so we can just convert this into an integer here
28+
num = int(num)
29+
30+
k = math.floor(math.log(num, 5))
31+
zeros = 0
32+
for i in range(1, k + 1):
33+
zeros = zeros + math.floor(num/math.pow(5, i))
34+
return zeros
35+
else:
36+
print("Factorial of a non-positive integer is undefined")
37+
38+
39+
if __name__ == "__main__":
40+
num_range = range(5, 10000, 10)
41+
zeros = []
42+
for num in num_range:
43+
num_zeros = trailing_zeros(num)
44+
zeros.append(num_zeros)
45+
for x, y in zip(num_range, zeros):
46+
print(x,y)
47+
plt.plot(num_range, zeros)
48+
plt.savefig('trailing_zeros.png')

0 commit comments

Comments
 (0)