def concat_primes(a, b): # are ab and ba (concatenations) prime global paircache if (a, b) in paircache: return paircache[(a, b)] result = lib.miller_rabin_verified(a*(10**(1+int(math.log10(b))))+b) and \ lib.miller_rabin_verified(b*(10**(1+int(math.log10(a))))+a) paircache[(a, b)] = result return result
def primes(n): assert n % 10 == 0 global steps, plistsize n2 = n * n for s in steps: # if any are composite if not lib.miller_rabin_verified(n2 + s): return False for s in range(1, steps[-1], 2): # make sure they are consecutive if (not s in steps) and lib.miller_rabin_verified(n2 + s): return False return True
def recurse(num,dsum): # assume arguments satisfy num % dsum == 0 global hsum, limit if num*10 >= limit: return if num % dsum == 0: # harshad number if lib.prime(num//dsum): # also strong harshad number num *= 10 if lib.miller_rabin_verified(num+1): hsum += num+1; print(':',num+1) if lib.miller_rabin_verified(num+3): hsum += num+3; print(':',num+3) if lib.miller_rabin_verified(num+7): hsum += num+7; print(':',num+7) if lib.miller_rabin_verified(num+9): hsum += num+9; print(':',num+9) else: num *= 10 for d in range(10): # recursively make right trunc harshad numbers if num % dsum == 0: recurse(num,dsum) num += 1 dsum += 1
def recurse(n, p): # recurse admissible numbers, p is last prime factor used global limit, pfset p += 1 # find next prime while not lib.miller_rabin_verified(p): p += 1 n *= p while n < limit: # find M>1 such than N+M is prime M = 2 while True: if lib.miller_rabin_verified(n + M): pfset.add(M) break M += 1 recurse(n, p) n *= p
def MNS(n,d): Nnd, Snd = 0, 0 for Mnd in range(n-1,0,-1): mask = ([0] * (n-Mnd)) + ([1] * Mnd) # 1=replaced digit, 0=other digits # mask is in big endian hasnextpermutation = True while hasnextpermutation: # try all mask permutations if d == 0 and mask[0] == 1: # cant have 0 at start of number hasnextpermutation = lib.lexico_next(mask) continue # use base 9 to select digits for replacement digits # if the b9 digit is b, b<d --> use itself, b>=d --> use b+1 for b9 in range(0,9**(n-Mnd)): # form numbers with the digits if d != 0 and mask[0] == 0 and b9 % 9 == 0: continue # cant put 0 at start of number num = 0 for m in mask: if m == 1: num = (num*10) + d # replaced digit else: # other digits b = b9 % 9 if b >= d: b += 1 num = (num*10) + b b9 //= 9 if lib.miller_rabin_verified(num): Nnd += 1 # count Snd += num # sum hasnextpermutation = lib.lexico_next(mask) if Nnd != 0: # found primes with Mnd instances of d print(': M(',n,',',d,') = ',Mnd,sep='') print(': N(',n,',',d,') = ',Nnd,sep='') print(': S(',n,',',d,') = ',Snd,sep='') return (Mnd, Nnd, Snd) assert 0 # should not reach here
def primes(n): assert n % 10 == 0 global steps n2 = n * n d = 3 # skip 2, odd steps so none are divisible by 2 while d <= n: # search for factors for s in steps: if (n2 + s) % d == 0: return False d += 2 # make sure there arent any primes in between, miller rabin test for s in range(1, steps[-1], 2): if (not s in steps) and lib.miller_rabin_verified(n2 + s): return False return True
# we must find the smallest n such that 2*n*p_n > modexceed # prime number theorem tells us n ~= p_n / ln(p_n) # pick a p_n estimate that gives 2*n*p_n ~= modexceed, then move up or down to # find the solution pn = 2 # approximate p_n with binary method while 2*int(pn/math.log(pn))*pn < modexceed: pn *= 2 pn //= 2 # step back add = pn // 2 while add != 0: pn += add if 2*int(pn/math.log(pn))*pn > modexceed: pn -= add add //= 2 print(': estimated p_n =',pn,'with n ~=',int(pn/math.log(pn))) # step back to find prime near p_n while not lib.miller_rabin_verified(pn): pn -= 1 print(': found prime value p_n =',pn) # count primes n = len(lib.list_primes2(pn)) print(': found n =',n) # count down if needed while 2*n*pn > modexceed: n -= 1 pn -= 1 # find previous prime while not lib.miller_rabin_verified(pn): pn -= 1 # go up to first n value that exceeds while 2*n*pn <= modexceed: n += 1 pn += 1 while not lib.miller_rabin_verified(pn): pn += 1 print(': found p_',n,' = ',pn,' with product 2*n*p_n = ',2*n*pn,sep='')
import libtkoz as lib maxn = 50 * 10**6 t = lambda n: 2 * n * n - 1 # very simple brute force, extremely slow, ~20min (pypy / i5-2540m) count = 0 for n in range(2, maxn + 1): if lib.miller_rabin_verified(t(n)): count += 1 print(count)