# Problem 53 from math_stuff import factorial total = 0 for n in range(23, 101): for r in range(1, n + 1): nr = factorial(n) / (factorial(r) * factorial(n - r)) if nr > 1_000_000: total += 1 print(total)
# Problem 20 # # n! means n × (n − 1) × ... × 3 × 2 × 1 # # For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, # and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. # # Find the sum of the digits in the number 100! from math_stuff import factorial fact_100 = factorial(100) my_sum = 0 for d in str(fact_100): my_sum += int(d) print(my_sum)
# Problem 34 # 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. # Find the sum of all numbers which are equal to the sum of the factorial of # their digits. # Note: as 1! = 1 and 2! = 2 are not sums they are not included. # 7*9! is a 7 digit number. 8*9! is also a 7 digit number so no answer will be # greater than 7 digits. from math_stuff import factorial def sum_of_fact_of_dig(number): """ Returns the sum of the factorial of numbers digits """ ret_sum = 0 for d in str(number): ret_sum += factorial(int(d)) return ret_sum upper_bound = 7 * factorial(9) all_sums = 0 for i in range(10,upper_bound + 1): if i == sum_of_fact_of_dig(i): all_sums += i print(all_sums)
def sum_of_fact_of_dig(number): """ Returns the sum of the factorial of numbers digits """ ret_sum = 0 for d in str(number): ret_sum += factorial(int(d)) return ret_sum
# Problem 24 v2 # # Instead of computing every permutation, this will only compute the # permutation at the requested position from math_stuff import factorial import time start_time = time.time() numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] numbers.sort position = 1_000_000 if position > factorial(len(numbers)): print('nope :(') exit() mutation = '' while len(numbers) > 1: j = position % factorial(len(numbers)) k = 0 index = 0 while k < j: index += 1 k = factorial(len(numbers) - 1) * index mutation += str(numbers.pop(index - 1)) mutation += str(numbers.pop()) print(mutation)