def nP4Quad(a, b): ''' Calculate number of primes for n**2 + an + b Where a, b are integers and n starts from 0 ''' n = 0 # Num of primes while True: if not eulerlib.isprime(n**2 + (a * n) + b): break n += 1 return n
def spiral(n): if n % 2 == 0: return None if n not in lookup: start = (n - 2)**2 + 1 corner = start + n - 2 C = [corner] for i in range(3): corner += n - 1 C.append(corner) r1 = (sum([1 for c in C if isprime(c)]), len(C)) r2 = spiral(n - 2) lookup[n] = (r1[0] + r2[0], r1[1] + r2[1]) return lookup[n]
def spiral(n): if n % 2 == 0: return None if n not in lookup: start = (n-2)**2 + 1 corner = start + n-2 C = [corner] for i in range(3): corner += n-1 C.append(corner) r1 = (sum( [1 for c in C if isprime(c)] ) , len(C)) r2 = spiral(n-2) lookup[n] = (r1[0] + r2[0], r1[1] + r2[1]) return lookup[n]
from itertools import permutations from eulerlib import isprime for n in reversed(xrange(1,10)): D = [str(d) for d in reversed(xrange(1,n+1))] found = False for p in permutations(range(len(D))): n = int(''.join([D[i] for i in p])) if isprime(n): print n found = True break if found: break
from itertools import permutations from eulerlib import isprime for n in reversed(xrange(1, 10)): D = [str(d) for d in reversed(xrange(1, n + 1))] found = False for p in permutations(range(len(D))): n = int(''.join([D[i] for i in p])) if isprime(n): print n found = True break if found: break
import eulerlib primes = [] for p in eulerlib.PrimesPlus(): primes.append(p) if p > 4000: # Only use first few primes break def check(n): ''' Check to see if a comp num satisfies CG's properties ''' for p in primes: for i in range(1, int(n)): if p + (2 * (i ** 2)) == n: return (p, i) return None for i in range(9, 10**5 + 1, 2): if not eulerlib.isprime(i) and check(i) == None: print(i) break
''' n = 0 # Num of primes while True: if not eulerlib.isprime(n**2 + (a * n) + b): break n += 1 return n ma, mb, mprimes = 0, 0, 0 factor = 100 # b must be prime becuase otherwise test fails when n == 0 brange = eulerlib.primes(1000) nbrange = [] for prime in brange: nbrange.append(prime * -1) brange += nbrange # Add neg version of the primes ### # Search the problem space... for a in range(-9 * factor, 10 * factor): for b in brange: if eulerlib.isprime(a + b + 1): # Cull at n == 1 (probably not needed) nprimes = nP4Quad(a, b) if nprimes > mprimes: ma, mb, mprimes = a, b, nprimes print("Solution: a = %d, b = %d, nprimes = %d, a*b = %d" % (ma, mb, mprimes, ma * mb))
# Solution to Project Euler #49 # # by Justin Ethier # import eulerlib import itertools # Find all 4 digit primes primes = [] for p in eulerlib.PrimesPlus(): if p > 10**3: if eulerlib.isprime(p): primes.append(p) if p > 10**4: break # Collect primes that are permutations of same digits seqs = {} for p in primes: bucket = list(str(p)) bucket.sort() key = int("".join(bucket)) if key in seqs: # Existing bucket, add p seqs[key].append(p) else: # New bucket, add p seqs[key] = [p] # Find solutions - that is, sequences of primes that increase by 3330