import PrimeNumber limit = 10**6 primes = PrimeNumber.get_primes( limit ) sieves = PrimeNumber.sieve( limit ) best_length = 0 best_prime = 0 for i in xrange( len(primes) ): cand = 0 for j in xrange( i, len(primes) ): cand += primes[j] if cand >= limit: break if sieves[ cand ]: length = j - i + 1 if best_prime == 0 or best_length < length: best_length = length best_prime = cand print best_prime, 'length is', best_length
import PrimeNumber, collections primes = PrimeNumber.get_primes( 100 ) queues = {} for p in primes: queues[p] = collections.deque() queues[p].append( p ) howmany = 1 while True: howmany += 1 mini = 0 for p in primes: if len( queues[p] ) == 0: continue if mini == 0 or queues[p][0] < queues[mini][0]: mini = p top = queues[mini][0] if howmany % 100 == 0: print howmany, top queues[mini].popleft() if top >= 10 ** 9: break for p in primes: if mini <= p and top * p <= 10 ** 9: queues[p].append( top * p ) print howmany
def get_numways( n ): primes = PrimeNumber.get_primes( n )[::-1] cache = {} ret = solve( 0, n, primes, cache ) return ret
import itertools, string, PrimeNumber # combinations_with_replacement is in ver 2.7 not 2.6 for c in itertools.combinations_with_replacement( string.digits, 4 ): if c[0] == '0': continue primes = [ int("".join(p)) for p in itertools.permutations( c ) if PrimeNumber.isprime( int("".join(p)) ) ] for three in itertools.combinations( primes, 3 ): three = map( int, three ) if three[2] - three[1] == three[1] - three[0] and three[2] - three[1] > 0: print three, "".join( map(str,three) )
import PrimeNumber, math bound = 50 * 10 ** 6 print bound primes = PrimeNumber.get_primes( int( math.sqrt(bound) ) ) s = set( [ p**2 + q**3 + r**4 for p in primes for q in primes if p**2+q**3 <= bound for r in primes if p**2+q**3+r**4 <= bound ] ) """ for sq in [ p**2 for p in primes ]: for cb in [ q**3 for q in primes if sq + q**3 <= bound ]: for qd in [ r**4 for r in primes if sq + cb + r**4 <= bound ]: s.add( sq + cb + qd ) """ print len( s )
import PrimeNumber, time start = time.time() isprime = PrimeNumber.sieve( 1000**2 + 1000*1000 + 1000 ) def num_cycle( a, b, n = 0 ): while True: if not isprime[ n**2 + a*n + b ]: break n += 1 return n max_n = 0 for a in xrange(-1000,1000): for b in xrange(-1000,1000): n = num_cycle( a, b ) if max_n < n: max_n = n print "update:", a*b, max_n print time.time() - start """ Sieve version : 4sec """
def num_cycle( a, b, n = 0 ): while True: if not PrimeNumber.isprime( n**2 + a*n + b ): break n += 1 return n