def euler0007(n): # n must be 10001 in order to solve the problem """ Find the 10001st prime. """ P,c = 2,0 while True: if euler.prime(P): c+=1 print P,c if c == n: return P,c P+=1
def euler0026(n): # n must be 1000 in order to solve the problem """ Find the value of d < 1000 for which 1/d contains the longest recurring cycle. """ IDC = '' N = 0 for x in range(1,n): if euler.prime(x): idc = (((euler.iDivision(x)).partition('('))[-1].partition(')'))[0] if len(idc) > len(IDC): IDC,N = idc,x return IDC,'r',N
def euler0027(n1,n2): # n1 and n2 must be 1000 in order to solve the problem """ Find a quadratic formula that produces the maximum number of primes for consecutive values of n. """ MAX,R = 0,0 for a in range(-n1,n1): for b in range(-n2,n2): n,prime = 0,True while prime: prime,n = euler.prime(((n*n)+(a*n)+(b))),n+1 n-=1 if n > MAX: MAX,R = n,(a,b,a*b) # print MAX,n,a,b return 'the answer is ',R
def main(): nd = 4 # do not limit to len(set(str(p))) == 4 primes = [p for p in prime(10**nd) if p > 10**(nd-1)] sorted_primes = sorted(primes, key=lambda i: sorted(str(i))) for k, it in groupby(sorted_primes, key=lambda i: sorted(str(i))): l = list(it) length = len(l) if length < 3: continue for i in xrange(1, length): b = l[i] # if any(2*b == a+c for a in la for c in lc): for a in l[:i]: for c in l[i+1:]: if 2 * b == a + c and b != 4817: return int(''.join(map(str, [a, b, c])))
l=[] l.append(1) l.append(tri(i)) for j in range(2,int(math.sqrt(tri(i)))+1): if(not tri(i)%j): l.append(j) l.append(tri(i)/j) if(len(l)>500): print i print tri(i) break """ # 3 second implementation L=[] for x in euler.prime(): L.append(x) if len(L) > 2000: break i = f = x = 0 while (f <= 500): i = i + 1 x = x + i f = 2 a = 1 j = 0 y = x while (y > 1): k = 1 while (x%(L[j]**k) == 0):
"""Smallest multiple""" from euler import prime N = 20 primes = list(prime(N+1)) for i in range(2, N+1): prime_factor = [] for p in primes: while i % p == 0: prime_factor.append(p) if i == p: break i = i / p if i == p: break for each in set(prime_factor): if prime_factor.count(each) > primes.count(each): primes.extend([each]*(prime_factor.count(each) - primes.count(each))) import operator import functools print(functools.reduce(operator.mul, primes))
import euler total = 0 x = 1 euler.primeCache(2000000) while euler.prime(x) < 2000000: total += euler.prime(x) x+=1 print total
def primeProof(x): xstr = str(x) for i in range(len(xstr)): for a in range(10): ystr = xstr[:i]+str(a)+xstr[i+1:] if euler.primeQ(int(ystr)): return False return True euler.primeCache(200000) squbes = [2*2*2*q*q for q in euler.primes] stopper = max(squbes) for p in xrange(1, len(euler.primes)): q = 2 while euler.prime(p)**2 * euler.prime(q)**3 < stopper: if p == q : q +=1 squbes.append(euler.prime(p)**2 * euler.prime(q)**3) q+=1 squbes.sort() squbes = filter(twohundred, squbes) i = 1 for can in squbes: if primeProof(can): print can, i if i == 200: print can break i+=1
import euler print euler.prime(10001)
def primeProof(x): xstr = str(x) for i in range(len(xstr)): for a in range(10): ystr = xstr[:i] + str(a) + xstr[i + 1:] if euler.primeQ(int(ystr)): return False return True euler.primeCache(200000) squbes = [2 * 2 * 2 * q * q for q in euler.primes] stopper = max(squbes) for p in xrange(1, len(euler.primes)): q = 2 while euler.prime(p)**2 * euler.prime(q)**3 < stopper: if p == q: q += 1 squbes.append(euler.prime(p)**2 * euler.prime(q)**3) q += 1 squbes.sort() squbes = filter(twohundred, squbes) i = 1 for can in squbes: if primeProof(can): print can, i if i == 200: print can break i += 1
import euler p=euler.prime(10000000) a=[] for x in p: if '1' in str(x): if '2' in str(x): if '3' in str(x): if '4' in str(x): if '5' in str(x): if '6' in str(x): if '7' in str(x):a.append(x) print a[-1]
#!/usr/bin/env python # By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we # can see that the 6th prime is 13. What is the 10001st prime number? import euler x=0 for prime in euler.prime(): x += 1 if x == 10001: print prime break
"""10001st prime""" from euler import prime N = 10001 M = 10**6 for index, number in enumerate(prime(M), start=1): if index == N: print number break
def truncatable_prime(n): for x in xrange(1, len(str(n))): if not prime(str(n)[x:]) or not prime(str(n)[:x]): return False return True
"""Largest prime factor""" from euler import prime #M = int(raw_input('Input the integer: ')) #if M < 2: print 'Invalid integer' M = 600851475143 N = M # p: prime number no bigger than N for p in prime(N+1): while N % p == 0: if N == p: print 'Largest prime factor of number {} is: {}'.format(M, p) raise SystemExit N = N / p
def circular_prime(n): for i in xrange(len(str(n))): if not prime(int(str(n)[i:] + str(n)[:i])): return False return True
def euler0003(n): # n must be 600851475143 in order to solve the problem """ Find the largest prime factor of a composite number. """ return ([x for x in euler.mult(n) if euler.prime(x)])
""" 10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10,001st prime number? """ from euler import euler_test, prime # function prime() moved to euler module if __name__ == "__main__": euler_test(7, prime(10001))
#!/usr/bin/env python # The number, 197, is called a circular prime because all rotations of # the digits: 197, 971, and 719, are themselves prime. There are # thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, # 73, 79, and 97. How many circular primes are there below one # million? import euler for p in euler.prime(): if p > 10**2:#6: break primes.add(p) circular = set() for p in primes:
import euler top = 1000 def lth(a, b): n = 0 while euler.primeQ(n**2 + a * n + b): n += 1 return n max = 0 for a in xrange(-1 * top + 1, top, 2): #odds only b = 1 while euler.prime(b) < top: p = euler.prime(b) if lth(a, p) > max: max, ans = lth(a, p), a * p b += 1 print ans
l=[] l.append(1) l.append(tri(i)) for j in range(2,int(math.sqrt(tri(i)))+1): if(not tri(i)%j): l.append(j) l.append(tri(i)/j) if(len(l)>500): print i print tri(i) break """ # 3 second implementation L = [] for x in euler.prime(): L.append(x) if len(L) > 2000: break i = f = x = 0 while (f <= 500): i = i + 1 x = x + i f = 2 a = 1 j = 0 y = x while (y > 1): k = 1 while (x % (L[j]**k) == 0):
"""Summation of primes""" import euler N = 2*10**6 print sum(i for i in euler.prime(N))
import euler top = 1000 def lth(a,b): n = 0 while euler.primeQ(n**2 + a*n + b): n+=1 return n max = 0 for a in xrange(-1*top+1, top,2): #odds only b = 1 while euler.prime(b)<top: p = euler.prime(b) if lth(a,p) > max: max, ans = lth(a,p), a*p b+=1 print ans
def largest_n_digit_pandigital_prime(n): p = permutations('987654321'[9-n:]) return max([int(''.join(x)) for x in p if prime(''.join(x))])