def check(iter): for pair in iter: p1 = primes[pair[0]] p2 = primes[pair[1]] if not euler.isprime(euler.concat(p1, p2)): return False if not euler.isprime(euler.concat(p2, p1)): return False return True
def trunc(n): num = str(n) for i in range(len(str(n))): cirnum = int(num[i:]) if not isprime(cirnum): return False for i in range(len(str(n))): cirnum = int(num[:len(num) - i]) if not isprime(cirnum): return False return True
def ispattern(i): ipatt = sorted(list(str(i))) if not isprime(i): return False perms = [] y = i + 3330 z = y + 3330 if sorted(list(str(y))) == ipatt and sorted(list(str(z))) == ipatt: if (isprime(y) and isprime(z)): print str(i) + str(y) + str(z) return True return False
def circular(n): num = str(n) for i in range(len(str(n))): cirnum = int(num[i:] + num[:i]) if not isprime(cirnum): return False return True
def getPrimes(dig, reploc): vals = [combine(dig, reploc, repval) for repval in range(10)] # filter leading zeros vals = [v for v in vals if v != None] vals = [euler.seqToInt(v) for v in vals] vals = [v for v in vals if euler.isprime(v)] return vals
def concatprimes(p): res = True for p in itertools.permutations(p,2): res = res and euler.isprime(int(str(p[0]) + str(p[1]))) if not res: break return res
def f(ratio): ntotal = 0 nprime = 0 for each in g6(): ntotal += 1 if isprime(each): nprime += 1 if nprime / ntotal < ratio: return int(each**.5)
def f(ratio): ntotal = 0 nprime = 0 for each in g6(): ntotal += 1 if isprime(each): nprime += 1 if nprime/ntotal < ratio: return int(each**.5)
def getPrimes( dig, reploc ): vals = [ combine(dig,reploc,repval) for repval in range(10) ] # filter leading zeros vals = [ v for v in vals if v != None ] vals = [ euler.seqToInt(v) for v in vals ] vals = [ v for v in vals if euler.isprime(v) ] return vals
def totient2(n): if euler.isprime(n): return n-1 else: factors = primeFactors(n) factors = [ fraction.Fraction(p-1,p) for p in factors ] factors = [ fraction.Fraction(n,1) ] + factors return reduce( lambda a,b:a*b, factors, fraction.Fraction(1,1) ).cancel().num
def nprimefactors(factors,size): """Return true has size distinct primefactors and""" if len(set(factors)) != size: # number of distinct primefactors not right ! return False for p in factors: if not euler.isprime(p): return False return True
def totient2(n): if euler.isprime(n): return n - 1 else: factors = primeFactors(n) factors = [fraction.Fraction(p - 1, p) for p in factors] factors = [fraction.Fraction(n, 1)] + factors return reduce(lambda a, b: a * b, factors, fraction.Fraction(1, 1)).cancel().num
def euler7(): numprimes = 1 counter = 1 limit = 10001 while numprimes < limit: counter += 2 if isprime(counter): numprimes += 1 print(counter)
def prime10001(): i = 2 cnt = 0 lastPrime = 0 while cnt < 10001: if euler.isprime((i)): cnt += 1 lastPrime = i i += 1 return lastPrime
def quadratic(a, b): n = 1 count = 1 while True: if isprime(n * n + a * n + b): n += 1 count += 1 else: break return count
def setup_primes(): primes = {} for p in range(LOWER_LIMIT, UPPER_LIMIT): if euler.isprime(p): k = euler.sort_digits(p) if k in primes: primes[k].append(p) else: primes[k] = [p] return primes
def search(): circularprimes = [] for n in range(2, 1000000): c = circulars(n) AllPrime = True for x in c: AllPrime = AllPrime and isprime(x) and not x in circularprimes if AllPrime: for x in c: circularprimes.append(x) print len(circularprimes) print circularprimes
def main(maxv): best = 2 f_best = 0 for n in range(390390, maxv): if euler.isprime(n) == False: f_n = f(n) if f_n > f_best: print 'possible', n, f_n best = n f_best = f_n if n % 1000 == 0: print '.' #n,f(n),'best',best,f_best
def PrimeFactorization(n): primefactors = []; a = 2; curr = n; while (curr > 1): if (curr % a == 0 and euler.isprime(a)): primefactors.append(a); curr = curr / a; a = 2; else: a = a + 1; return primefactors;
def main(maxv): best = 2 f_best = 0 for n in range(390390,maxv): if euler.isprime(n) == False: f_n = f(n) if f_n > f_best: print 'possible',n,f_n best = n f_best = f_n if n % 1000 == 0: print '.'#n,f(n),'best',best,f_best
def num_divisors(x): """ Count number of divisors. """ prime_factors = collections.defaultdict(int) # Find prime factors. while not euler.isprime(x): for i in range(2, int(math.ceil(math.sqrt(x))) + 1): if x % i == 0: prime_factors[i] += 1 x = x / i break prime_factors[x] += 1 return reduce(operator.mul, [x + 1 for x in prime_factors.values()])
def tleft(n): seq = euler.intToSeq(n) return [seq[:i] for i in range(1, len(seq) + 1)] def tright(n): seq = euler.intToSeq(n) return [seq[i:] for i in range(0, len(seq))] def tall(n): return tleft(n) + tright(n) res = [] for n in euler.sieve(4000): if n > 7: seq = tall(n) seq = [euler.seqToInt(x) for x in seq] seq = [euler.isprime(x) for x in seq] val = reduce(lambda a, b: a and b, seq, True) if val: res.append(n) print n, seq print 'soln', sum(res) + 739397 #if sum([ 1 for x in seq if not euler.isprime(x) ]) == 0: # print n
def primeGenerator(n): for i in xrange(2, n): if euler.isprime(i): yield i
Consequently 56003, being the first member of this family, is the smallest prime with this property. Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family. """ maxcnt = 0 maxpattern = [] for length in range(1,7): for p in itertools.product('123456789*', repeat=length): pattern = ''.join(p) if '*' in pattern and not pattern == '*'*len(pattern): primecount = 0 for d in range(0,10): if euler.isprime(int(pattern.replace('*',str(d)))): primecount += 1 if primecount > maxcnt: maxpattern = [pattern] maxcnt = primecount elif primecount == maxcnt: maxpattern.append(pattern) print "Max. primes = {}, pattern(s) = {}".format(maxcnt, maxpattern) for pattern in maxpattern: primes = [] for d in range(0,10): s = pattern.replace('*',str(d)) if not s.startswith('0') and euler.isprime(int(s)): primes.append(s) print pattern, primes
def test_quadratic(self): self.assertTrue(isprime(quadratic(1, 0, 1)))
def primeGenerator(n): for i in xrange(2, n ): if euler.isprime(i): yield i
PRIMECEILING=10000 """ Solved: [13, 5197, 5701, 6733, 8389] 26033 """ def concatprimes(p): res = True for p in itertools.permutations(p,2): res = res and euler.isprime(int(str(p[0]) + str(p[1]))) if not res: break return res primes = [p for p in range(3, PRIMECEILING) if euler.isprime(p)] for p1 in primes: base = [p1,0,0,0,0] for p2 in primes: if p2>p1: base = [p1,p2,0,0,0] # print base if concatprimes(base[:2]): for p3 in primes: if p3>p2: base = [p1,p2,p3,0,0] # print base if concatprimes(base[:3]): for p4 in primes: if p4>p3: base = [p1,p2,p3,p4,0]
from euler import prime, isprime c = 1 while True: c += 2 thisisit = True # assume c is the target if not isprime(c): for p in prime: if p == 2: continue if p < c: a = int(((c - p) / 2)**.5) if c == p + 2 * a * a: thisisit = False # c is not #print '%d == %d + 2 * %d * %d' % (c, p, a, a) break else: break if thisisit: print c break
"""We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? """ from itertools import permutations from euler import isnpandigital, isprime digits = [ str(i) for i in range(1,10) ] found = False n = 9 pdprimes = [] while not found and n > 0: print "{}-digit pandigitals".format(n) for c in permutations(digits[0:n]): x = int(''.join(c)) if isnpandigital(str(x),n) and isprime(x): pdprimes.append(x) found = len(pdprimes) > 0 n -= 1 print pdprimes, max(pdprimes)
def searchprimes(): return [str(n) for n in range(2, CEILING) if isprime(n)]
n = 1 while True: if n == 1: base = 1 yield [base] else: vertices = [base + 2 * i * (n-1) for i in range(1,5)] base = vertices[3] yield vertices n += 1 if '__name__ == __main__': primeratio = 0.0 # ratio of primes over all elements iteration = 0 # keep track of iterations prime = 0 # start without primes elements = 0 # number of elements on diagonals vertices = diagonals() # generator object while primeratio > 0.1 or iteration < 2: v = vertices.next() for n in v: if euler.isprime(n): prime += 1.0 iteration += 1 elements += len(v) primeratio = prime/elements print "Iteration: {}, side length: {}, ratio: {}/{}={}".format( iteration, 2*iteration-1, prime, elements, primeratio)
def test_isprime(self): self.assertEqual(euler.isprime(1), False) self.assertEqual(euler.isprime(2), True) self.assertEqual(euler.isprime(3), True) self.assertEqual(euler.isprime(4), False) self.assertEqual(euler.isprime(5), True)
if not seq: return [seq] else: temp = [] for k in range(len(seq)): part = seq[:k] + seq[k+1:] for m in permute(part): temp.append(seq[k:k+1] + m) return temp """ there is a cute little formula about divisibilty: A number is divisible by 3 if the sum of the digits is divisible by 3. So, with pandigital numbers: sum(1:2) = 3 sum(1:3) = 6 sum(1:4) = 10 sum(1:5) = 15 sum(1:6) = 21 sum(1:7) = 28 sum(1:8) = 36 sum(1:9) = 45 from this, a pandigital prime has to be 7 digits or 4 digits, we were given the larget 4 digit pandigital prime, so we just need to check the 7 digit numbers """ for i in permute('7654321'): if euler.isprime(int(i)): print i break
def validPair(a, b): i = concatPrimes(a, b) j = concatPrimes(b, a) if euler.isprime(i) and euler.isprime(j): return True return False
import euler maxcnt = 0 for a in range(-999,1000): for b in range(-999,1000): if a != 0 and b != 0: cnt = 0 for n in range(80): v = n*n + a*n + b if v < 0: break #print 'v',v if euler.isprime(v): cnt += 1 else: break if cnt > maxcnt: print a,b,cnt maxcnt = cnt
#!/usr/bin/env python import euler num_primes = 0 number = 1 while num_primes != 10001: if euler.isprime(number): last_prime = number num_primes += 1 number += 1 print last_prime
def PD(n): return sum([1 for d in diffs(n) if isprime(d)])
def primeGenerator(n): for i in xrange(2, n): if euler.isprime(i): yield i def rotations(n): res = [] sn = str(n) for i in range(len(sn)): res.append(sn) snd = sn[1:] + sn[0] sn = snd res = [int(r) for r in res] return res cnt = 0 for p in primeGenerator(1000000): allprime = True for rot in rotations(p): if not euler.isprime(rot): allprime = False break if allprime: cnt += 1 print p, cnt, rotations(p), allprime print 'soln', cnt
import euler import os def primeGenerator(n): for i in xrange(2, n ): if euler.isprime(i): yield i def rotations(n): res = [] sn = str(n) for i in range(len(sn)): res.append( sn ) snd = sn[1:] + sn[0] sn = snd res = [ int(r) for r in res ] return res cnt = 0 for p in primeGenerator( 1000000 ): allprime = True for rot in rotations(p): if not euler.isprime(rot): allprime = False break if allprime: cnt += 1 print p,cnt,rotations(p),allprime print 'soln',cnt
import euler for i in range(9): print '----' lst = range(1, i + 2) lst.reverse() for perm in euler.permutations(lst): v = euler.seqToInt(perm) if euler.isprime(v): print v break
def oldhamming(n, m): return max(euler.primeFactors(n)) <= m import math def generate(primes, c): if primes == []: #yield [],1 yield 1 else: for i in xrange(0, int(math.ceil(math.log(c, primes[0])))): #for lst, res in generate(primes[1:], c/(primes[0]**i)+1 ): for res in generate(primes[1:], c / (primes[0]**i) + 1): final = primes[0]**i * res if final < c: #yield [i]+lst, final yield final primes = [x for x in range(2, 100 + 1) if euler.isprime(x)] primes.reverse() c = 0 for item in generate(primes, 10**9): if c % 10000 == 0: print item c += 1 print c + 1
import euler def tleft(n): seq = euler.intToSeq(n) return [ seq[:i] for i in range(1,len(seq)+1) ] def tright(n): seq = euler.intToSeq(n) return [ seq[i:] for i in range(0,len(seq)) ] def tall(n): return tleft(n) + tright(n) res = [] for n in euler.sieve(4000): if n > 7: seq = tall(n) seq = [ euler.seqToInt(x) for x in seq ] seq = [ euler.isprime(x) for x in seq ] val = reduce( lambda a,b : a and b, seq, True ) if val: res.append(n) print n,seq print 'soln',sum(res) + 739397 #if sum([ 1 for x in seq if not euler.isprime(x) ]) == 0: # print n
def isprime(n): if n < maxprime: return n in lowPrimes else: # return euler.isprime_slow(n) return euler.isprime(n)
import itertools import euler ''' This is apparently pretty far off the money wrt the 'right' way to do this ''' primes = [x for x in xrange(1000) if euler.isprime(x)] def numPrimeSums(x): def impl(x, maxval, stack): primesLessThanMax = [y for y in primes if y <= maxval] cnt = 0 for p in primesLessThanMax: if x - p == 0: cnt += 1 elif x - p > 0: cnt += impl(x - p, p, stack + [p]) return cnt return impl(x, x, []) # by guesswork print numPrimeSums(70) print numPrimeSums(71)
''' 9-digit pandigitals are not prime, as 1+2+3+4+5+6+7+8+9=45 is divided by 3 8-digit pandigitals are not prime, as 1+2+3+4+5+6+7+8=36 is divided by 3 so check from 7-digit pandigitals ''' from euler import isprime, permutate digits = range(7, 0, -1) for d in digits: for p in permutate(range(d, 0, -1)): # permutate in desc order n = int(''.join(map(str, p))) #n = sum(p[i]*(10**i) for i in range(len(p))) if isprime(n): print n break
from euler import isprime import time def quadratic(a, b): n = 1 count = 1 while True: if isprime(n * n + a * n + b): n += 1 count += 1 else: break return count start = time.time() result = 0 lenab = -1 for a in range(-999, 1000, 1): for b in range(2, 1000, 1): # when n = 0, we get b ; b should be prime if isprime(b) and a + b > 0 and isprime(1 + a + b): data = quadratic(a, b) if data > lenab: result = a * b lenab = data spend = time.time() - start print "The result is %s and take time is %f" % (result, spend)
''' 9-digit pandigitals are not prime, as 1+2+3+4+5+6+7+8+9=45 is divided by 3 8-digit pandigitals are not prime, as 1+2+3+4+5+6+7+8=36 is divided by 3 so check from 7-digit pandigitals ''' from euler import isprime, permutate digits = range(7,0,-1) for d in digits: for p in permutate(range(d,0,-1)): # permutate in desc order n = int(''.join(map(str,p))) #n = sum(p[i]*(10**i) for i in range(len(p))) if isprime(n): print n break
#cool simple solution... from euler import isprime all = set(i for i in range(3,10000,2)) created = set() primes = set(i for i in range(1,10000) if isprime(i)) for a in primes: for b in range(1,100): created.add(a + 2*(b**2)) composite = all - primes lst = list(composite-created) lst.sort() print(lst)
def primeFactors(n): s = set() for f in euler.factors(n): if euler.isprime(f): s.add(f) return s
from euler import prime_sieve, isprime import time def is_square(n): x = int((n / 2)**0.5) if 2 * x * x == n: return True else: return False start = time.time() num = 3 result = 0 while True: flag = False if not isprime(num): for item in prime_sieve(num): if is_square(num - item): flag = True break if not flag: result = num break num += 2 spend = time.time() - start print "The result is %s and take time is %f" % (result, spend)
import euler count = 0 for i in range(2,1001): if euler.isprime(i): circular_prime = True for j in range(len(list(str(i)))): number = list(str(i)) chosen_character = number.pop(j) new_prime = 0 if number != []: other_characters = number new_prime = int(str(chosen_character) + ''.join(other_characters)) else: new_prime = int(str(chosen_character)) if not euler.isprime(new_prime): circular_prime = False break print(new_prime) if circular_prime: count += 1 #print(i) print("Count: " + str(count))
from euler import isprime import time start = time.time() n = 0 for i in range(2, 4 * 10**6): if isprime(i): n += 1 if n > 10000: break time_take = time.time() - start print "The sum is %s and take time is %f" % (i, time_take)
from euler import isprime from itertools import permutations maxprime=2 pand=0 for n in xrange(9, 0, -1): perms = permutations(xrange(1, n + 1), n) for p in perms: pan = ''.join([str(c) for c in p]) if isprime(int(pan)): if pan > maxprime: maxprime = pan print maxprime
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953. Which prime, below one-million, can be written as the sum of the most consecutive primes? ==== MAX ==== Max at 997651, being: [7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931] ==== """ import euler UPPERLIMIT=1000000 # Generating a list of primes up to 10^6 takes < 10 s primes = [n for n in range(2, UPPERLIMIT) if euler.isprime(n)] # loop over primes primeterms = {} maxprime = primes[len(primes)-1] for i_start in range(0, len(primes)): # scan elements from 0 to i-1 to see if *CONSECUTIVE* elements add up to # the current prime. i_end = i_start sum_cp = 0 print "Scanning prime at idx: {}, value: {}".format(i_start, primes[i_start]) while i_end < len(primes)-1 and sum_cp < maxprime: #if i_end % 5000: # print "Index in scan: {}".format(i_end) sum_cp = sum_cp + primes[i_end] if i_start != i_end and sum_cp in primes:
def isprime(n): if n < maxprime: return n in lowPrimes else: # return euler.isprime_slow(n) return euler.isprime(n)
def goldbach(n): for sq in squares(n): if euler.isprime(n - sq): #print n,int(math.sqrt(sq/2)),n-sq return True return False
import euler num = 600851475143 r = num factors = [] root = (num / 2) + 1 for i in xrange(2, root + 1): if euler.isprime((i)): if r > 1: while (r / i) * i == r: r = (r / i) factors.append(i) else: break if len(factors) == 0: print("Number %d is prime" % num) else: print("Largest prime factor = %d" % factors[-1]) print(factors)
def oddComposite(): i = 3 while 1: if not euler.isprime(i): yield i i += 2