Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. from itertools import permutations, combinations
  2.  
  3.  
  4. def is_prime(num):
  5. for i in range(2, int(num ** 0.5) + 1):
  6. if num % i == 0:
  7. return False
  8.  
  9. return True
  10.  
  11.  
  12. def propagate(cur_nums, level=0):
  13. if level == 5:
  14. print(cur_nums)
  15.  
  16. if sum(cur_nums) < propagate.min:
  17. propagate.min = sum(cur_nums)
  18.  
  19. for num in pairs:
  20. if num not in cur_nums:
  21. valid = True
  22.  
  23. for i in cur_nums:
  24. if num not in pairs[i]:
  25. valid = False
  26. break
  27.  
  28. if valid:
  29. lst = list(cur_nums)
  30. lst.append(num)
  31. propagate(lst, level + 1)
  32. propagate.min = 100**100
  33.  
  34. primes = [x for x in range(2, 10000) if is_prime(x)]
  35. cur_min = 100**100
  36. pairs = {key: [] for key in primes}
  37.  
  38. for a, b in combinations(primes, 2):
  39. if is_prime(int(str(a) + str(b))) and is_prime(int(str(b) + str(a))):
  40. pairs[a].append(b)
  41. pairs[b].append(a)
  42.  
  43. propagate([])
  44. print(propagate.min)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement