def isTruncPrime(n): divisor = 1 # Check first digit is prime while True: trunc = n // divisor if trunc <= 0: break if trunc == 1 or not isPrime(trunc): return False divisor *= 10 while (n % divisor) > 0: trunc = n % divisor if trunc == 1 or not isPrime(n % divisor): return False divisor /= 10 return True
def hasCriteria(n): limit = int(sqrt(n // 2)) for dSq in [2 * i**2 for i in xrange(limit, 0, -1)]: diff = n - dSq if diff == 1 or isPrime(diff): return True return False
def challenge041(): # loop through number of digits highest = 0 for n in [4, 7]: chars = string.join([str(c) for c in xrange(1, n + 1)], "") for potential in [int(p) for p in permutate(chars) if isPrime(int(p))]: if potential > highest: highest = potential return highest
def challenge046(): n = 3 while True: # Find doubled squares lower than n if not hasCriteria(n): return n n += 2 while n == 1 or isPrime(n): n += 2
def challenge027(): limit = 999 aMax = 0 bMax = 0 maximum = 1 bS = list(sievedPrimes(limit + 1)) for b in bS: for a in xrange(-b, limit + 1): n = 1 while True: f = n * (n + a) + b if not isPrime(f): break n += 1 if n > maximum: maximum = n aMax = a bMax = b return aMax * bMax