Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BIN +18.7 KB explorations/trailing_zeros/trailing_zeros.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions 43 explorations/trailing_zeros/trailing_zeros.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This program will print the number of trailing zeros the factorial
# of a number
# The formula and the associated explanations are available at:
# https://brilliant.org/wiki/trailing-number-of-zeros/

# Notes on the Python implementation are available on https://doingmathwithpython.github.io

import math


def is_positive_integer(x):
try:
x = float(x)
except ValueError:
return False
else:
if x.is_integer() and x > 0:
return True
else:
return False


def trailing_zeros(num):
if is_positive_integer(num):
# The above function call has done all the sanity checks for us
# so we can just convert this into an integer here
num = int(num)

k = math.floor(math.log(num, 5))
zeros = 0
for i in range(1, k + 1):
zeros = zeros + math.floor(num/math.pow(5, i))
return zeros
else:
print("Factorial of a non-positive non-integer is undefined")


if __name__ == "__main__":
fact_num = input(
"Enter the number whose factorial's trailing zeros you want to find: "
)
num_zeros = trailing_zeros(fact_num)
print("Number of trailing zeros: {0}".format(num_zeros))
48 changes: 48 additions & 0 deletions 48 explorations/trailing_zeros/trailing_zeros_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This program will print the number of trailing zeros the factorial
# of a number
# The formula and the associated explanations are available at:
# https://brilliant.org/wiki/trailing-number-of-zeros/

# Notes on the Python implementation are available on https://doingmathwithpython.github.io

import math
import matplotlib.pyplot as plt


def is_positive_integer(x):
try:
x = float(x)
except ValueError:
return False
else:
if x.is_integer() and x > 0:
return True
else:
return False


def trailing_zeros(num):
if is_positive_integer(num):
# The above function call has done all the sanity checks for us
# so we can just convert this into an integer here
num = int(num)

k = math.floor(math.log(num, 5))
zeros = 0
for i in range(1, k + 1):
zeros = zeros + math.floor(num/math.pow(5, i))
return zeros
else:
print("Factorial of a non-positive integer is undefined")


if __name__ == "__main__":
num_range = range(5, 10000, 10)
zeros = []
for num in num_range:
num_zeros = trailing_zeros(num)
zeros.append(num_zeros)
for x, y in zip(num_range, zeros):
print(x,y)
plt.plot(num_range, zeros)
plt.savefig('trailing_zeros.png')
Morty Proxy This is a proxified and sanitized view of the page, visit original site.