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 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 range(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 range(min_n, max_n): if gcd_it(numerator, denominator) == 1: nfractions += 1 return nfractions
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 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 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 range(len(primes)): if primes[i] > lim_root: break ans += bisect_left(primes, truediv(upper_bound, primes[i])) - i return ans
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 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 test_use_prime_list(self): """ testing if the dictionary recreation is good """ assert list(prime_gen(200, kprimes=p_lt100)) == p_gt100_lt200
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 functools import lru_cache from pupy.decorations import cprof from pupy.maths import divisors_gen from pupy.amazon import prime_gen upperlim = 1000000 primes = {p for p in prime_gen(upperlim)} chainlengths = {} perfects = set() mean_numbers = set() @lru_cache(maxsize=None) def proper_divisors_gen(n): return sum(number for number in divisors_gen(n) if number < n) @lru_cache(maxsize=None) def is_perfect(n): return n == proper_divisors_gen(n)
def p010(): return sum(p for p in prime_gen(2000000))
def test_small_ub_large_known_primes(self): """ test_pupy handling an upper_bound < max prime """ assert list(prime_gen(50, kprimes=p_lt100)) == p_lt50
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 p007(nth_prime=10001): count = 0 for p in prime_gen(): count += 1 if count == 10001: return p
def test_prime_seive_gen(self): """ test_pupy if can generate primes below 100 """ assert p_lt100 == [p for p in prime_gen(100)]