[PYTHON] Project Euler 34

problem

145 is an interesting number. 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of the numbers so that the sum of the factorials of each digit matches itself.

Note: 1! = 1 and 2! = 2 must not be included in the sum. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2034

Answer

I made a function to determine whether a number is the sum of factorials.

def is_wa_of_kaijo(i,kaijos):
  s = 0
  for j in str(i):
    s+= kaijos[int(j)]
  if i == s:
    return True
  else:
    return False

Using this function, we performed an 100% search within the range considered to be the maximum value, but it was very slow. Since n> = 7 and 10 ** n -1> 9! * N, it is sufficient to search the range up to 9! * 7.

def main():
  MAX = math.factorial(9)*7
  kaijos = [math.factorial(x) for x in range(0,10)]
  ans = 0
  for i in range(3,MAX):
    if is_wa_of_kaijo(i,kaijos):
      ans += i
  print ans

As a different approach, I first created a factorial sum set type object of up to 7 factors, and tried to perform an 100% search on the factorial sum set type object. At least the decision part should be faster because the search population is smaller. I tried to remember the factors when creating the factorial sum list, but I gave up the implementation because it became a very complicated process. The factorial of zero is 1, but at the beginning, it is set to 0 in order to retain the value of the list without addition.

def main2():
  kaijos = [0] + [math.factorial(x) for x in range(1,10)]
  kaijos2 = set(x1+x2 for x1 in kaijos for x2 in kaijos)
  kaijos3 = set(x1+x2 for x1 in kaijos for x2 in kaijos2)
  kaijos4 = set(x1+x2 for x1 in kaijos2 for x2 in kaijos2)
  kaijos7 = set(x1+x2 for x1 in kaijos3 for x2 in kaijos4)
  kaijos[0] = 1
  ans = -3
  for i in kaijos7:
    if is_wa_of_kaijo(i,kaijos):
      ans += i
  print ans

I also considered a version that slightly increases the inclusion notation of the generator.

def main3():
  kaijos = [0] + [math.factorial(x) for x in range(1,10)]
  kaijos7 = set(x1+x2+x3+x4+x5+x6+x7 for x1 in kaijos for x2 in kaijos for x3 in kaijos for x4 in kaijos for x5 in kaijos for x6 in kaijos for x7 in kaijos)
  kaijos[0] = 1
  print len(kaijos7)
  ans = -3
  for i in kaijos7:
    if is_wa_of_kaijo(i,kaijos):
      ans += i
  #print ans

Execution time main2() 0.484 main3() 18.011

I will investigate later what is the cause of this difference in execution time.

Recommended Posts

Project Euler 37
Project Euler 47
Project Euler 31
Project Euler 4
Project Euler 38
Project Euler 26
Project Euler 8
Project Euler 22
Project Euler 19
Project Euler 50
Project Euler 33
Project Euler 32
Project Euler 43
Project Euler 35
Project Euler 36
Project Euler 24
Project Euler 48
Project Euler 45
Project Euler 6
Project Euler 44
Project Euler 39
Project Euler 40
Project Euler 49
Project Euler 29
Project Euler 27
Project Euler 41
Project Euler 18
Project Euler 13
Project Euler 30
Project Euler 16
Project Euler 14
Project Euler 34
Project Euler 25
Project Euler15 "Lattice Path"
Project Euler 2 Acceleration 2.21 Save microseconds.
Project Euler Original Method Group 1
Functional programming in Python Project Euler 1
Project Euler 10 "Sum of Prime Numbers"
[Note] Project Euler in Python (Problem 1-22)
Functional programming in Python Project Euler 3
Project Euler # 5 "Minimum Multiples" in Python
Project Euler 4 Attempt to speed up
Functional programming in Python Project Euler 2
Project Euler # 4 "Maximum Palindrome" in Python
Project Euler 9 Retention of calculation results
Project Euler # 3 "Maximum Prime Factors" in Python
Project Euler # 11 "Maximum Product in Grid" in Python
Project Euler # 16 "Sum of Powers" in Python
Project Euler # 9 "Special Pythagorean Triple" in Python
Project Euler # 14 "Longest Collatz Sequence" in Python
Project Euler # 17 "Number of Characters" in Python
Project Euler # 1 "Multiples of 3 and 5" in Python
Project Euler # 8 "Maximum Product in Number String" in Python
Project Euler # 10 "sum of prime numbers" in Python
Project Euler 28 Inefficient Answer Proposal Create "Spiral Numbers"
Project Euler 4 Coding with a new approach fails.
Project Euler # 12 "High Divisibility Triangular Number" in Python
[Day 2] Project generation