def get_valid(): ''' This function returns: A list of lists ('valid_primes') where all primes in each sub-list are valid and have the same number of digits (starting at 1 digit in index 0). All valid primes are contained within the lists A dictionary ('valid_digits') with primes as keys and the number of digits in the corresponding keys as values. A dictionary ('valid_bits') with primes as keys and a bit representation of which digits (1-9) are in the corresponding key as values. A 1 in the nth bit means the key contains the digit n+1 A prime is considered valid if it is < 10**8 and has no zeroes or repeating digits. ''' primes = primes_to(10**8) valid_digits = defaultdict(int) valid_bits = defaultdict(int) valid_primes = [[] for _ in range(8)] shifts = [(1<<n) for n in range(10)] for n in primes: found = 0 orig = n digits = 0 while n: n, d = divmod(n, 10) if d == 0 or (shifts[d-1] & found): break found |= shifts[d-1] digits += 1 else: valid_primes[digits-1] += [orig] valid_digits[orig] = digits valid_bits[orig] = found return valid_primes, valid_digits, valid_bits
def main(): ''' Driver function ''' total = 0 mult = {1: 3, 3: 1, 5: 7, 7: 5} for p in primes_to(10**8)[2:]: total += (mult[p % 8] * p - 3) // 8 print(total)
def semiprime_below(end): ''' Determine how many semiprimes are below 'end' ''' total = 0 primes = primes_to(end) for i, p in enumerate(primes[:bisect(primes, int(end**0.5))]): total += bisect(primes, end // p) - i return total
def main(): ''' Driver function ''' end = 10**1 mod = 10**9 + 7 primes = primes_to(end) pi = prime_count(primes, end) values = k_values(pi, set(primes), end) print(product(values, mod))
def main(): ''' Driver function ''' total = 3 end = int(1e8) primes = set(primes_to(end)) total += check_lines(6, end, 36, primes) total += check_lines(30, end, 36, primes) total += check_lines(10, end, 12, primes) print(total)
def main(): ''' Driver function ''' end = 10**5 primes = primes_to(2 * end + 1) points = point_primes(end, primes) vals = [[n**2 + 1, 1] for n in range(end)] for n in range(10**2): result = find_point(vals, points, n) if not result: print(n)
def main(): ''' Driver function ''' end = 10**7 primes = primes_to(end // 2) total = 0 for i, p1 in enumerate(primes): if p1**2 > end: break for p2 in primes[i + 1:]: if p1 * p2 > end: break total += M(p1, p2, end) print(total)
def get_cube_fill_bases(end): ''' Returns the set of all possible prime factorizations (disregarding exponents) for cube-filled numbers up to 'end' ''' primes = primes_to(int(end**(1 / 3)) + 1) all_bases = cur_bases = {(p, ) for p in primes} while any(cur_bases): new_bases = set() for factors in cur_bases: start = bisect(primes, factors[-1]) highest_val = int(end**(1 / 3) / reduce(mul, factors, 1)) stop = bisect(primes, highest_val) new_bases |= {factors + (p, ) for p in primes[start:stop]} cur_bases = new_bases all_bases |= new_bases return all_bases
def all_S_to(n): ''' Returns a list S such that S[x] equals S(x) (mod 10**9) in the above definition of S for all x <= n ''' primes = primes_to(n) S = [0 for _ in range(n + 1)] S[0] = 1 mod = 10**9 # Dynamic programming solution based on finding the number of solutions to # a linear equation given the coefficients of the variables. Primes are # coefficients, the value of a variable in a given solution is the exponent # of its prime coefficient in the prime factorization of the number encoded # by the solution for p in primes: for i in range(p, n + 1): # If p*S[i-p] were changed to just S[i-p], this would count the # number of solutions to the linear equation S[i] = (S[i] + p * S[i - p]) % mod return S
def sum_S_to(n): ''' Returns the sum of values of S (as described in the problem) for all consecutive primes p1 and p2, p2 > p1 and 5 <= p1 <= n ''' total = 0 primes = primes_to(n) # Get p2 for last p1 primes += [primes_nth(len(primes) + 1)] # The lowest positive value of n that satisfies both n = 0 (mod p2) and # n = p1 (mod 10**k) (k being the number of digits in p1) is S for that # pair of primes. The function (easily derived from modular relations) # n(x) = pow_10*(x*p2 - p1*pow_10_mult_inv) + p1 # (pow_10 is 10**k and pow_10_mult_inv is the multiplicative inverse of # 10**k mod p2) produces all valid values of n for a given p1,p2 pair for p1, p2 in zip(primes[2:-1], primes[3:]): p1_digits = int(log(p1, 10)) + 1 pow_10 = 10**p1_digits pow_10_mult_inv = pow(pow_10, -1, p2) # Find the lowest value of x for which n(x) is positive x = int((-p1 / pow_10 + p1 * pow_10_mult_inv) / p2) + 1 total += pow_10 * (x * p2 - p1 * pow_10_mult_inv) + p1 return total
def S(n): ''' Implementation of the S(n) function described above ''' n_sqr = n * n total = 0 primes = primes_to(n) primes_set = set(primes) for i, a in enumerate(primes): # m is an integer such that if f(x) = (x*m)**2 for any natural x, f(x) # returns the xth perfect square divisible by a+1 # From: https://math.stackexchange.com/a/4084884/530956 m = 1 for p, e in factorize(a + 1): m *= p**(ceil(e / 2)) m_sqr = m * m x = a // n val = m_sqr // (a + 1) c = x * x * val - 1 while (c := c + val * (2 * x + 1)) < n: x += 1 if (c in primes_set) and ((b := x * m - 1) in primes_set) and (a < b < c): total += a + b + c