Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This code find the number of trailing zeroes in a factorial.
- # The number of zeroes is the lesser of "factors of two" and "factors of five" in all the numbers of the factorial expansion.
- # (Since there are more twos than fives, and every two, times five, equals a 10, which leads to one trailing zero, it depends only on factors of five.)
- import math
- import random
- twos = 0
- fives = 0
- remainders_two = []
- remainders = []
- factorial = 1000000
- #
- for i in range(60):
- factorial = random.randint(10,10000000)
- print("Factorial of", factorial, "has about", factorial/4, "zeroes.")
- factemp=factorial
- while True:
- twos += factemp // 2
- remainders_two.append(math.floor(factemp % 2))
- factemp = factemp / 2
- if factemp < 1:
- break
- # factorial = (10**i)*10
- factemp=factorial
- while True:
- fives += factemp // 5
- remainders.append(math.floor(factemp % 5))
- factemp = factemp / 5
- if factemp < 1:
- break
- # factorial = (10**i)*1000000
- # print("For the numbers 1-10^"+str(math.log10(factorial)))
- # print(factorial-twos, "factors of two less than expected 1")
- print("Calculated zeroes:", fives)
- print("Remainders:", remainders)
- print("Sum of remainders, divided by 4:", sum(remainders)/4)
- print("--")
- print("Calculated twos:", twos)
- print("Remainders:", remainders_two)
- print("Sum of remainders, divided by 1:", sum(remainders_two))
- if sum(remainders)/4 == factorial/4-fives and sum(remainders_two) == factorial-twos:
- print("MATCH!")
- else:
- print("DOES NOT MATCH!")
- break
- print()
- twos = 0
- fives = 0
- remainders_two.clear()
- remainders.clear()
Advertisement
Add Comment
Please, Sign In to add comment