Advertisement
benlloydjones

Project Euler 43

Jul 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. def findPrimesTo(n):
  2.     primes = []
  3.     for potentialPrime in range(2, n + 1, 1):
  4.         isPrime = True
  5.         for prime in primes:
  6.             if potentialPrime % prime == 0:
  7.                 isPrime = False
  8.                 break
  9.         for divisor in range(2, int(potentialPrime ** 0.5) + 1, 1):
  10.             if potentialPrime % divisor == 0:
  11.                 isPrime = False
  12.                 break
  13.         if isPrime:
  14.             primes.append(potentialPrime)
  15.     return primes
  16.  
  17. def PE43():
  18.     from itertools import permutations
  19.     primes = findPrimesTo(17)
  20.     possibles = []
  21.     gooduns = []
  22.     dog = permutations('0123456789', 10)
  23.     for cat in dog:
  24.         collar = ''
  25.         for mouse in cat:
  26.             collar += mouse
  27.         possibles.append(int(collar))
  28.     for x in possibles:
  29.         rope = str(x)
  30.         good = True
  31.         for y in range(1, 8, 1):
  32.             if int(rope[y:y + 3]) % primes[y - 1] != 0:
  33.                 good = False
  34.                 break
  35.         if good:
  36.             gooduns.append(x)
  37.     return sum(gooduns)
  38.  
  39. PE43()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement