Guest User

Factorial Trailing Zeroes Calculator

a guest
Apr 19th, 2025
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | Science | 0 0
  1. # This code find the number of trailing zeroes in a factorial.
  2. # The number of zeroes is the lesser of "factors of two" and "factors of five" in all the numbers of the factorial expansion.
  3. # (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.)
  4.  
  5. import math
  6. import random
  7.  
  8. twos = 0
  9. fives = 0
  10. remainders_two = []
  11. remainders = []
  12. factorial = 1000000
  13.  
  14. #
  15.  
  16. for i in range(60):
  17.     factorial = random.randint(10,10000000)
  18.     print("Factorial of", factorial, "has about", factorial/4, "zeroes.")
  19.     factemp=factorial
  20.     while True:
  21.         twos += factemp // 2
  22.         remainders_two.append(math.floor(factemp % 2))
  23.         factemp = factemp / 2
  24.         if factemp < 1:
  25.             break  
  26.        
  27. #    factorial = (10**i)*10
  28.     factemp=factorial
  29.     while True:
  30.         fives += factemp // 5
  31.         remainders.append(math.floor(factemp % 5))
  32.         factemp = factemp / 5
  33.         if factemp < 1:
  34.             break  
  35.    
  36. #    factorial = (10**i)*1000000
  37.    
  38. #    print("For the numbers 1-10^"+str(math.log10(factorial)))
  39. #    print(factorial-twos, "factors of two less than expected 1")
  40.     print("Calculated zeroes:", fives)
  41.     print("Remainders:", remainders)
  42.     print("Sum of remainders, divided by 4:", sum(remainders)/4)
  43.     print("--")
  44.     print("Calculated twos:", twos)
  45.     print("Remainders:", remainders_two)
  46.     print("Sum of remainders, divided by 1:", sum(remainders_two))
  47.     if sum(remainders)/4 == factorial/4-fives and sum(remainders_two) == factorial-twos:
  48.         print("MATCH!")
  49.     else:
  50.         print("DOES NOT MATCH!")
  51.         break
  52.     print()
  53.     twos = 0
  54.     fives = 0
  55.     remainders_two.clear()
  56.     remainders.clear()
Advertisement
Add Comment
Please, Sign In to add comment