def compute(limit): pres = [] def rec(pi, rest, coef, pres=pres): ret = rest * coef for pj in range(pi, len(pres)): if pres[pj][0][0] > rest: break for p1, c1 in pres[pj]: if p1 > rest: break ret += rec(pj + 1, rest // p1, coef * c1) return ret for prime in prime_sieve(int(limit ** 0.5)): q, prev, e = prime * prime, 1, 2 pre = [] while q <= limit: g = gcd(e * (q // prime), q) if g - prev: pre.append((q, g - prev)) q *= prime e += 1 prev = g pres.append(pre) print(rec(0, limit, 1) - 1)
def compute(limit): phi = [i for i in range(0, limit + 1)] primes = prime_sieve(limit) for p in primes: for j in range(1, int(limit / p) + 1): phi[j * p] *= (p - 1) / p return int(sum(phi[2:]))
def compute(limit): period = 1 for i in prime_sieve( limit )[::-1]: # Run through all primes backwards to find largest first while pow(10, period, i) != 1: # pow(a, b ,c) == a ** b % c period += 1 if i - 1 == period: return i
def find_perms(limit): primes = prime_sieve(limit) ans = [] for i in primes: for j in range(1000, limit // 3): if i + j in primes and i + 2 * j in primes and sorted(str(i)) == sorted(str(i + j)) == sorted(str(i + 2 * j)): ans.append((i, i + j, i + 2 * j)) return ans
def compute(limit): primes = prime_sieve(20) current_max_n = 1 for p in primes: if current_max_n * p > limit: return current_max_n else: current_max_n *= p return current_max_n
def compute(limit): for prime in prime_sieve(limit): if prime > limit // 10: s = str(prime) last_digit = s[5:6] if s.count('0') == 3 and eight_prime_family(s, '0'): return s if s.count('1') == 3 and last_digit != '1' and eight_prime_family(s, '1'): return s if s.count('2') == 3 and eight_prime_family(s, '2'): return s
def compute(limit): n_max = 0 for b in prime_sieve(limit): for a in range(-b, 0, 2): n = 1 while m(n, a, b) > 0 and is_prime(m(n, a, b)): n += 1 if n > n_max: n_max, prod = n, a * b return prod
def consecutive_primes(prime_limit, index_max, pos_max): primes = prime_sieve(prime_limit) max_terms = 6 max_prime = 41 for j in range(pos_max): for i in range(j + index_max, j, -1): current_sum = sum(primes[j:i]) if current_sum in primes and i > max_terms: max_terms = i max_prime = current_sum return max_prime
def compute(goal): primes = prime_sieve(100) r, d = 1, 1 for prime in primes: for i in range(2, prime): if (r * i / float(d * i - 1)) < goal: return d * i r *= prime - 1 d *= prime if (r / float(d - 1)) < goal: return d else: raise Exception("Increase length of prime list to obtain result.")
def compute(limit): primes = prime_sieve(int(1.2 * limit**0.5)) del primes[:int(0.6 * len(primes))] min_q, min_n, i = 2, 0, 0 for p_1 in primes: i += 1 for p_2 in primes[i:]: n = p_1 * p_2 if n > limit: return min_n phi = (p_1 - 1) * (p_2 - 1) q = n / float(phi) if is_perm(phi, n) and min_q > q: min_q, min_n = q, n
def compute(n, p_ind=0): primes = prime_sieve(int(1.1 * floor(n))) res = n for p_ind in range(p_ind, len(primes)): p = primes[p_ind] pow = p**2 if pow > n: break elif p == 2: fact = pow else: fact = p res += (fact - 1) * compute(n // pow, p_ind + 1) last = fact exp = 3 pow *= p while pow <= n: if exp % p == 1: pow *= p exp += 1 elif exp % p == 0: fact = p**exp else: fact = p**(exp - 1) res += (fact - last) * compute(n // pow, p_ind + 1) last = fact pow *= p exp += 1 return res - 1
def compute(position): """Finds the positionth prime in a list of primes.""" return prime_sieve(position * 15)[position - 1]
def compute(limit): return sum(prime_sieve(limit + 1))
import sys from helpers import prime_sieve if __name__ == "__main__": if len(sys.argv) == 2: n = int(sys.argv[1]) else: n = 2000000 print(sum(prime_sieve(n)))