def test_small_primes(self): """ testing the generator with smaller primes """ assert list(prime_gen(2)) == [2] assert list(prime_gen(3)) == [2, 3] assert list(prime_gen(5)) == [2, 3, 5] assert list(prime_gen(7)) == [2, 3, 5, 7] assert list(prime_gen(11)) == [2, 3, 5, 7, 11]
def S_M(max_n): primes = [p for p in prime_gen(max_n)] squares = [n * n for n in range(max_n + 1)] def M(n): # print("_") # print(n) if primes[-1] >= n == primes[bisect_left(primes, n)]: return 1 if squares[bisect_left(squares, n)] == n: return 1 for a in range(n - 1, -1, -1): right = (a % n) # left = (a*a)%n sq = squares[a] left = squares[a] % n if right == left: return a assert 4 == M(6) # for n in range(1, 1000): # print(M(n)) m_sum = 0 for n in tqdm(range(1, max_n + 1)): # print("_") # print(n, M(n)**2, M(n)) m_sum += M(n) m_sum -= 1 print("MSUM", m_sum) return m_sum
def fractions_in_range(max_d=12000, gtd=3, ltd=2): """number of reduced proper fractions in a range Args: max_d: maximum denominator gtd: greater than denominator ltd: less than denominator Returns: int: no. reduced proper fractions, frac, such that frac > (1/gtd) and and frac < (1/ltd) """ primes = set(p for p in prime_gen(max_d)) nfractions = 0 for denominator in xrange(4, max_d+1): min_n = denominator//gtd+1 max_n = denominator//ltd+1 if denominator in primes: nfractions += (max_n-min_n) else: for numerator in xrange(min_n, max_n): if gcd_it(numerator, denominator) == 1: nfractions += 1 return nfractions
def thingy(limit): print("____") print(limit) D = {} count = 1 primes = set(i for i in (prime_gen(limit))) # print("\nprimes made") def divisors_thing(divs, n): return all(i in primes for i in map(partial(funnn, n=n), divs)) for q in tqdm(range(0, limit+1)): if q not in D: D.setdefault(q+q, set()).add(q) else: if q%4 == 2: first_half = sorted(D[q]) if divisors_thing(first_half[0:1+len(first_half)//2], q): # print(q) count += q # print("SOMETHING", something) for p in D[q]: D.setdefault(p+q, set()).add(p) D.setdefault(q+q, set()).add(q) del D[q] return count
def F(n): primes = {p for p in prime_gen(n)} d = defaultdict(set) for p1, p2 in combinations(primes, 2): if is_connected(p1, p2): d[p1].add(p2) d[p2].add(p1) two_relatives = set() print(d) def _traverse(pn, rchain, cmax): if rchain[-1]==pn: return True possrels = list(sorted((el for el in d[rchain[-1]] if el not in rchain and el<=pn),reverse=True)) if len(possrels)==0: return False for pr in possrels: if pr > cmax: two_relatives.add(pr) cmax = pr tc = copy(rchain)+(pr,) # print(tc) if _traverse(pn, tc, cmax): return True return False # print(possrels) for p in primes: if p not in two_relatives: _traverse(p, (2, ), 2) print(two_relatives) print(two_relatives) print(sum(two_relatives), sum(primes))
def p187(upper_bound=10**8): primes = list(n for n in prime_gen(1 + upper_bound // 2)) lim_root = int(sqrt(upper_bound) + 1) ans = 0 for i in xrange(len(primes)): if primes[i] > lim_root: break ans += bisect_left(primes, truediv(upper_bound, primes[i])) - i return ans
def p069(): # we are looking for the number divisible by the most consecutive # prime numbers pprod = 1 for p in prime_gen(): if pprod * p > 1000000: return pprod pprod *= p
def p134_cy(lim=1000000): from bib.prime_pairs import prime_pair_cy primes = [p for p in prime_gen(lim + 5) if p > 3] mod10e = 10 multipliers = [] for i in tqdm(range(len(primes) - 1), ascii=True): if primes[i] > mod10e: mod10e *= 10 multipliers.append(prime_pair_cy(primes[i], primes[i + 1], mod10e)) return sum(a * b for a, b in zip(primes[1:], multipliers))
def s(n): primes = [] prime_sieve_limit = int(n // 2) + 1 # plus two cause idk ret_sum = 0 for p in prime_gen(prime_sieve_limit): mul_list = primes[0:bisect_right(primes, n // p)] ret_sum += sum(m(ppp, p, n) for ppp in mul_list) primes.append(p) return ret_sum
def prime_power_triples(below): primes = [p for p in prime_gen(int(sqrt(below) + 1))] n_primes = len(primes) hermy = [0] * below for first in range(n_primes): for second in range(n_primes): for third in range(n_primes): pt = power_triple( [primes[third], primes[second], primes[first]]) if pt > below: break hermy[pt] = 1 return sum(hermy)
def consecutive_prime_sum(upper_bound): primes_list = [p for p in prime_gen(upper_bound)] primes_set = set(primes_list) longest_seq = 1 max_sum = 1 for p in range(len(primes_list), 0, -1): cur_sum = primes_list[p-1] cur_seq_length = 1 for j in range(p-1, 0, -1): cur_sum += primes_list[j-1] cur_seq_length += 1 if cur_sum > upper_bound: break if cur_seq_length >= longest_seq and cur_sum < upper_bound and cur_sum in primes_set: max_sum = cur_sum longest_seq = cur_seq_length return longest_seq, max_sum
def prime_pair_sets(set_size): pairs = defaultdict(set) past_primes = [] def look_up_is_prime_pair_set(comb): return set.intersection(*[pairs[c] for c in comb]) for p in prime_gen(): for pp in past_primes: if check_pair(p, pp): pairs[p].add(pp) pairs[pp].add(p) pairs[p].add(p) pairs[pp].add(pp) for comb in combinations(pairs[p], set_size - 1): sssss = look_up_is_prime_pair_set(comb) if len(sssss) == set_size: return sum(sssss) past_primes.append(p)
def test_use_prime_list(self): """ testing if the dictionary recreation is good """ assert list(prime_gen(200, kprimes=p_lt100)) == p_gt100_lt200
form a chain of five numbers: 12496 → 14288 → 15472 → 14536 → 14264 (→ 12496 → ...) Since this chain returns to its starting point, it is called an amicable chain. Find the smallest member of the longest amicable chain with no element exceeding one million. """ from tqdm import tqdm from bib.maths import divisors_gen from bib.amazon_prime import prime_gen upperlim = 1000000 primes = {p for p in prime_gen(upperlim)} chainlengths = {} perfects = set() mean_numbers = set() def proper_divisors_gen(n): return (number for number in divisors_gen(n) if number < n) def is_perfect(n): return n == sum(proper_divisors_gen(n)) def chaingang(start_n): the_chain = []
def p010(): return sum(p for p in prime_gen(2000000))
def p187a(upper_bound=10**8): primes = list(n for n in prime_gen(1 + upper_bound // 2)) for c in combinations_with_replacement(primes, 2): print(c)
def psr(remainder_max): for n, p in enumerate(prime_gen()): rem = 2 * (n + 2) * p if rem > remainder_max: return n + 2
def p035(): return sum(1 for p in prime_gen(10**6) if is_circ_prime(p))
def p007(nth_prime=10001): count = 0 for p in prime_gen(): count += 1 if count == 10001: return p
def test_small_ub_large_known_primes(self): """ test_fest handling an upper_bound < max prime """ assert list(prime_gen(50, kprimes=p_lt100)) == p_lt50
""" Counting numbers with at least four distinct prime factors less than 100 Problem 268 It can be verified that there are 23 positive integers less than 1000 that are divisible by at least four distinct primes less than 100. Find how many positive integers less than 1016 are divisible by at least four distinct primes less than 100. """ from itertools import combinations from bib import xrange from bib.amazon_prime import prime_gen from bib.listless import reduce_product primes = [p for p in prime_gen(100)] print(primes) lt100 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] n = 0 for i in xrange(100, 1000): t = 0 for p in lt100: if i%p == 0: t += 1 if t > 3: print(i, t) n += 1 print(n)
def test_prime_seive_gen(self): """ test_fest if can generate primes below 100 """ assert p_lt100 == [p for p in prime_gen(100)]
Problem 77 It is possible to write ten as the sum of primes in exactly five different ways: 7 + 3 5 + 5 5 + 3 + 2 3 + 3 + 2 + 2 2 + 2 + 2 + 2 + 2 What is the first value which can be written as the sum of primes in over five thousand different ways? """ from bib.amazon_prime import is_prime, prime_gen p = [p for p in prime_gen(5000)] print(sum(p)) maxp = 1548136 pp = [p for p in prime_gen(maxp)] # print(pp) def prime_sums(n): # this is the same as my coin sums code # primes = [p for p in range(2, n) if is_prime(p)] ok_primes = [i for i in p if i < (n + 1)] sums = [0] * (n + 1) sums[0] = 1 for prime in ok_primes: for i in range(prime, n + 1):