Skip to content

Instantly share code, notes, and snippets.

@mdtareque
Created August 17, 2021 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdtareque/eb790c89efb4fca7e0db42e4a5726d97 to your computer and use it in GitHub Desktop.
Save mdtareque/eb790c89efb4fca7e0db42e4a5726d97 to your computer and use it in GitHub Desktop.
p27
import os
primes=set()
def p(n):
while n%2 == 0:
n = n//2
if (n == 1): return True
limit = n//2
for j in range(3, limit, 2):
while n%j == 0:
n = n//j
return n != 1
def preComputePrimes():
global primes
fname="primesTill100K.txt"
if os.path.isfile(fname):
with open('primesTill100K.txt', 'r') as filehandle:
primes = set([int(cur.rstrip()) for cur in filehandle.readlines()])
print("primes loaded form file ", len(primes))
return
print("no cache, computing primes")
primes.add(2)
for j in range(3, 100000, 2):
if p(j):
primes.add(j)
print("count of primes ", len(primes))
print("writing to file")
with open('primesTill100K.txt', 'w') as filehandle:
filehandle.writelines("%d\n" % p for p in primes)
def eval(a, b, x):
val = x*x + a*x + b
if val > max(primes):
print("VAL exceeding primes max",val,a,b)
return val
def numOfConsecutivePrimes(a,b):
primeCount = 0
for n in range(0,1000):
if eval(a,b,n) in primes:
primeCount+=1
return primeCount
def run_p27(u):
max = 0
ans = 0
for a in range(-u+1, u):
print("running for a ",a)
for b in range(-u, u+1):
cur = numOfConsecutivePrimes(a,b)
if cur > max:
max = cur
ans = a*b
print(a,b, a*b, max);
print("p27 answer: ",ans)
preComputePrimes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment