37.py 860 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from math import sqrt
  2. def prime(n):
  3. """Checks whether a number is prime"""
  4. n = int(n)
  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 digremoval(n):
  12. """Returns a list with all the truncated versions of the inputted integer from both left and right"""
  13. n = str(n)
  14. nlist = [n]
  15. for i in range(1, len(n)):
  16. nlist.append(n[i::])
  17. nlist.append(n[:i:])
  18. return nlist
  19. def truncatable(n):
  20. """Returns whether all truncated versions of the integer are prime"""
  21. n = str(n)
  22. # nlist is the list of all truncates
  23. nlist = digremoval(n)
  24. for i in nlist:
  25. if not prime(i):
  26. return False
  27. return True
  28. truncprimes = []
  29. numgen = 8
  30. # Loops until there are 11 truncatable primes
  31. while len(truncprimes) < 11:
  32. if truncatable(numgen):
  33. truncprimes.append(numgen)
  34. numgen += 1
  35. print sum(truncprimes)