def pgen(): yield 2 p = 3 while True: if lib.prime(p): yield p # print(': prime',p) p += 2
def recurse(digits, nums, prevdiv): # digits numbers made, last divider global uniquesets, count if prevdiv == len(digits): count += 1 # found a set with increasing primes else: # make prime number with next digits and place divider num = 0 for d in range(prevdiv, len(digits)): # try each digit num = num * 10 + digits[d] # only make sets with primes in increasing order for uniqueness if lib.prime(num) and (len(nums) == 0 or num > nums[-1]): recurse(digits, nums + [num], d + 1)
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 is_prime(n): if n <= pclim: return n in pcset return lib.prime(n)
import libtkoz as lib import math N = 600851475143 # brute force basically, factoring by numbers up to square root largest = 0 for d in range(1, int(math.sqrt(N)) + 1): if N % d == 0: if lib.prime(N // d): # n/d decreases so if prime, its the largest largest = N // d break if lib.prime(d): # d is increasing largest = d print(largest) # faster algorithm, dividing out smaller prime factors n = N while n % 2 == 0: n //= 2 print(': factor', 2) d = 3 while d <= int(math.sqrt(n)): while n % d == 0: n //= d print(': factor', d) d += 2 print(n)
import libtkoz as lib # brute force, generate lexicographic permutations decreasing # by considering digit sums we can show that only # 7*8/2=28 and 4*5/2=10 are not divisible by 3, then there is 1*2/2=1 # 1 is not prime so the largest must have 4 or 7 digits for l in [7, 4]: digits = list(range(l, 0, -1)) print(': permuting', digits) found_prime = False while True: # go through all permutations # test primality if lib.prime(int(''.join(str(d) for d in digits))): print(''.join(str(d) for d in digits)) found_prime = True break i1 = len( digits) - 1 # find position of digit breaking right->left decrease while i1 != 0 and digits[i1 - 1] <= digits[i1]: i1 -= 1 i1 -= 1 if i1 == -1: print(': finished all', l, 'length permutations') break i2 = len(digits) - 1 # find largest smaller digit to swap with it while digits[i2] >= digits[i1]: i2 -= 1 digits[i1], digits[i2] = digits[i2], digits[i1] # swap i1 += 1 # sort starting from here i2 = len(digits) - 1 while i1 < i2:
from functools import reduce solutions = 1000 # 1/x+1/y=1/n, we must have x>n and y>n so use x=n+x0 and y=n+y0 # n(x+y)=xy --> n(2n+x0+y0)=n^2+nx0+ny0+x0y0 --> n^2=x0y0 # we need the smallest perfect square with enough distinct ways to factor it # into x0*y0 such that x0<=y0. every factor x0<=n corresponds to a y0=n/x0 so # n^2 must have 1000 divisors <= n, or 1999 overall (exclude counting n twice) # any perfect square will have an odd number of divisors (counting divisors with # the product of 1+ prime exponents) so we need a n^2 with >2*solutions divisors primes = [] # find enough primes so that 3^(prime count)>2*solutions n = 2 while 3**len(primes) <= 2*solutions: if lib.prime(n): primes.append(n) n += 1 print(': generated primes',primes) smallest = reduce(lambda x,y:x*y, (p*p for p in primes)) print(': initial product of the squares is',smallest) print(':',smallest,lib.divisors2(smallest),lib.prime_factorization(smallest)) # recurse on the prime list to find a smaller number with over 2000 divisors # order exponents in decreasing order, 2^a*3^b has (a+1)(b+1) divisors so order # a >= b (put the greater exponent on the smaller number def recurse(number,primeindex,divisors,lastexp): global primes, smallest, solutions if divisors > 2*solutions: print(':',number,divisors,lib.prime_factorization(number), lib.divisors2(number)) smallest = min(smallest,number)
import libtkoz as lib limit = 2000000 psum = 0 pcount = 0 # could use sum(lib.list_primes(...)) # brute force approach will use loop to prevent using memory to store primes # took ~6 sec (i7-7600u) for n in range(limit): if lib.prime(n): psum += n pcount += 1 print(': prime count', pcount) print(psum)
import libtkoz as lib nval = 10 #10000 modulus = 10**100 #10**9+7 # generate the Pn and Cn sequences pn = [] cn = [] i = 2 while len(pn) < nval or len(cn) < nval: ip = lib.prime(i) if ip and len(pn) < nval: pn.append(i) if not ip and len(cn) < nval: cn.append(i) i += 1 pn = list(lib.digital_root(i) for i in pn) cn = list(lib.digital_root(i) for i in cn) print(': P_n', pn) print(': C_n', cn) superint = [] pi = 0 # indexes in pn an cn ci = 0 while pi < len(pn): # start by picking smaller digit unless we can maximize intersecting digits # by picking the other if ci < len(cn): if pn[pi] < cn[ci]: superint += [pn[pi]] pi += 1 elif cn[ci] < cn[pi]: superint += [cn[ci]]
def is_prime(n): global primelim, primecache if n > primelim: return lib.prime(n) return n in primecache
def is_prime(n): if n <= cachemax: return n in cache return lib.prime(n)
def prime(n): global ptable global ptablemax return (n <= ptablemax and n in ptable) or lib.prime(n)
def is_prime(p): global primecachesize global primecache if p < primecachesize: return p in primecache else: return lib.prime(p)
import libtkoz as lib ratio = 0.10 # prime ratio must get smaller than this # brute force, test all primes in spiral, takes ~7.5 sec (i5-2540m) step = 2 # difference between numbers in next loop # side length is step+1 and number visited is 2*step+1 (+1 for center 1) spiralnum = 1 # number to test for primality primes = 0 while True: # try 4 corner numbers in next loop for i in range(4): spiralnum += step if lib.prime(spiralnum): primes += 1 if primes / (2 * step + 1) < ratio: # prime ratio small enough print(step + 1) break step += 2
import libtkoz as lib # phi(n) is n * product of every (1-1/p) so n/phi(n) is 1/(product of (1-1/p)) # to maximize the ratio, we need as many distinct primes as possible # multiply distinct primes in increasing order until multiplying the next one # would exceed the limit, the solution is that (and all multiples of it within # the limit have the same ratio) maxn = 1000000 prod = 1 p = 2 while True: if lib.prime(p): prod *= p if prod > maxn: prod //= p break p += 1 if prod * 2 <= maxn: print(': multiple solutions, choosing the smallest') print(':', prod, '/ phi(n) =', prod / lib.totient(prod)) print(prod)