Advertisement
danchaofan

Euler #61

Dec 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from itertools import permutations
  2.  
  3. octa, hepta, hexa, penta, sq, tri = [], [], [], [], [], []
  4. for a in range(1, 10**10):
  5.     temp = int(a*(3*a-2))
  6.     if temp < 1000:
  7.         continue
  8.     if temp > 9999:
  9.         break
  10.     octa.append(temp)
  11. for a in range(1, 10**10):
  12.     temp = int(a*(5*a-3)/2)
  13.     if temp < 1000:
  14.         continue
  15.     if temp > 9999:
  16.         break
  17.     hepta.append(temp)
  18. for a in range(1, 10**10):
  19.     temp = int(a*(2*a-1))
  20.     if temp < 1000:
  21.         continue
  22.     if temp > 9999:
  23.         break
  24.     hexa.append(temp)
  25. for a in range(1, 10**10):
  26.     temp = int(a*(3*a-1)/2)
  27.     if temp < 1000:
  28.         continue
  29.     if temp > 9999:
  30.         break
  31.     penta.append(temp)
  32. for a in range(1, 10**10):
  33.     temp = int(a**2)
  34.     if temp < 1000:
  35.         continue
  36.     if temp > 9999:
  37.         break
  38.     sq.append(temp)
  39. for a in range(1, 10**10):
  40.     temp = int(a*(a+1)/2)
  41.     if temp < 1000:
  42.         continue
  43.     if temp > 9999:
  44.         break
  45.     tri.append(temp)
  46.  
  47.  
  48. def check(shape, num):
  49.     for x in range(int(str(num)[-2:] + "01"), int(str(num)[-2:] + "99") + 1):
  50.         if x in shape:
  51.             return x
  52.     return 0
  53.  
  54. choices = list(permutations([tri, sq, penta, hexa, hepta]))
  55. for b in octa:
  56.     print(octa.index(b))
  57.     for d in choices:
  58.         answer, current, nums = b, b, 0
  59.         for c in d:
  60.             if check(c, current) > 0:
  61.                 current = check(c, current)
  62.                 answer += current
  63.                 nums += 1
  64.             else:
  65.                 break
  66.         if (nums == 5) and (check(octa, current) == b):
  67.             print(answer)
  68.             quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement