49.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from math import sqrt
  2. from math import fabs
  3. def prime(n):
  4. """Returns a boolean indicating whether an integer is a prime"""
  5. if n <= 1:
  6. return False
  7. for i in range(2, int(sqrt(n) + 1)):
  8. if n % i == 0:
  9. return False
  10. return True
  11. def isPermutation(a, b):
  12. """Returns a boolean indicating whether the two numbers are permutations of each other"""
  13. a = str(a)
  14. b = str(b)
  15. return sorted(a) == sorted(b)
  16. primesList = []
  17. # Create a list of all primes between the two values
  18. for i in range(1000, 10000):
  19. if prime(i):
  20. primesList.append(i)
  21. midpointList = []
  22. # Create a list of all equidistant triples of primes
  23. for i in primesList:
  24. for j in primesList:
  25. if int((i + j)/2) in primesList:
  26. midpointList.append([i, int((i + j)/2), j, fabs((i - j)/2)])
  27. permutationList = []
  28. # Create a list of all triples that are permutations of each other
  29. for sequence in midpointList:
  30. if isPermutation(sequence[0], sequence[1]) and sequence[0] != sequence[1]:
  31. if isPermutation(sequence[1], sequence[2]) and sequence[1] != sequence[2]:
  32. permutationList.append(sequence)
  33. # Print out resulting 12-digit number
  34. for i in permutationList:
  35. for j in range(len(i)):
  36. i[j] = str(i[j])
  37. print "".join(i[:-1])