fork download
  1. # 1
  2. def diagsum(n):
  3. return sum([4 * x**2 - 6 * x + 6 for x in range(3, n + 1) if x % 2 != 0]) + 1
  4.  
  5. print('Sum of diagonal elements:', diagsum(1013))
  6. print()
  7.  
  8. # 2
  9. from itertools import permutations
  10.  
  11. def numsolutions():
  12. num = 0
  13. for perm in permutations('0123456789', 5):
  14. if perm[0] == '0' or perm[1] == '0' or perm[2] == '0':
  15. continue
  16. r, s, x, z, v = perm
  17. if int(r + s + r + x) + int(z + v + v) == int(s + r + x + s):
  18. print('[' + r + s + x + z + v + ']:', r + s + r + x, '+', z + v + v, '=', s + r + x + s)
  19. num += 1
  20. return num
  21.  
  22. print('Solutions:', numsolutions())
  23. print()
  24.  
  25. # 3
  26. import collections
  27.  
  28.  
  29. def same_permutation(a, b, c):
  30. d = collections.defaultdict(int)
  31. for x in str(a):
  32. d[x] += 2
  33. for x in str(b):
  34. d[x] -= 1
  35. for x in str(c):
  36. d[x] -= 1
  37. return not any(d.values())
  38.  
  39.  
  40. def lower():
  41. i, i2, i5 = 1, 2, 5
  42. while not same_permutation(i, i2, i5):
  43. i += 1
  44. i2, i5 = i * 2, i * 5
  45. return i
  46.  
  47.  
  48. l = lower()
  49. print(l, ': 2x = ', 2 * l, ': 5x = ', 5 * l)
  50.  
Success #stdin #stdout 0.5s 28384KB
stdin
Standard input is empty
stdout
Sum of diagonal elements: 693520565

[12795]: 1217 + 955 = 2172
[23895]: 2328 + 955 = 3283
Solutions: 2

142857 : 2x =  285714 : 5x =  714285