def main(): p = Prime() p.sieve(100000000) primes = p.primes() print("sieve done") search(p, tuple(), 5, 0) print(p.counter)
def main(): p = Prime() n = 1000000 sieve = [False] * n naive = [False] * n with Timer('naive'): for i in range(n): naive[i] = is_prime(i) with Timer('sieve'): p.sieve(n) for i in range(n): sieve[i] = p.is_prime(i)
def search(p: Prime, t: tuple, r: int, first: int): if len(t) < r: for i, p1 in enumerate(islice(p.primes(), first, 2000)): t1 = t + (p1, ) if all(concatable_primes(p, t1)): search(p, t1, r, first=i) else: print(t)
def search(p: Prime, t: tuple, r: int, first: int): if len(t) < r: for i, p1 in enumerate(islice(p.primes(), first, 2000)): t1 = t + (p1,) if all(concatable_primes(p, t1)): search(p, t1, r, first=i) else: print(t)
def test_generate1(self): """prime generator 1""" p = Prime() self.assertEqual(self.c, list(p[:len(self.c)]))
def test_init4(self): """remove multiples of 2-3-5-7""" p = Prime(4) self.assertEqual(self.c[:4], list(p.table)) self.assertEqual(self.c[4:30], list(islice(p.sieve, 26))) self.assertEqual(121, next(p.sieve))
def test_init3(self): """remove multiples of 2-3-5""" p = Prime(3) self.assertEqual(self.c[:3], list(p.table)) self.assertEqual(self.c[3:15], list(islice(p.sieve, 12))) self.assertEqual(49, next(p.sieve))
def test_init2(self): """remove multiples of 2-3""" p = Prime(2) self.assertEqual(self.c[:2], list(p.table)) self.assertEqual(self.c[2:9], list(islice(p.sieve, 7))) self.assertEqual(25, next(p.sieve))
def test_init1(self): """remove multiples of 2""" p = Prime(1) self.assertEqual(self.c[:1], list(p.table)) self.assertEqual(self.c[1:4], list(islice(p.sieve, 3))) self.assertEqual(9, next(p.sieve))
def test_factorize2(self): """prime factorization 2""" p = Prime() self.assertEqual([99991, 100003], list(p.factorize(9999399973)))
from itertools import count, combinations from fractions import Fraction, gcd from tqdm import tqdm from primes import Prime prime = Prime() prime.sieve(1000000) def factor(n): idx = 0 primes = prime.primes() while n > 1: idx = next(i for i in count(idx) if n % primes[i] == 0) yield primes[idx] n //= primes[idx] def product(iterable): tmp = 1 for x in iterable: tmp *= x return tmp def facit(d): return sum(1 for n in range(1, d) if gcd(n, d) == 1) def R(d): factors = set(factor(d)) resilient = d s = -1
def test_generate2(self): """prime generator 2""" p = Prime() self.assertEqual(131071, p[12250])
def main(): prime = Prime() #print(prime.is_prime(987654321)) print(max(p for p in iter_pandigitals() if prime.is_prime(p)))
from fractions import Fraction from tqdm import tqdm from primes import Prime prime = Prime() prime.sieve(1000000) def product(v): r = 1 for e in v: r *= e return r def φ(n): factors = set(prime.factor(n)) return n * product(f - 1 for f in factors) // product(factors) def main(i): def maximizer(x): return Fraction(x, φ(x)) print(max((n for n in tqdm(range(2, i + 1))), key=maximizer)) main(1000000)
def test_isprime3(self): """primality test 3""" p = Prime() self.assertTrue(99999989 in p) self.assertTrue(999999999989 in p)
def test_isprime4(self): """primality test 4""" p = Prime() self.assertTrue(9999399973 not in p)
def test_factorize1(self): """prime factorization 1""" p = Prime() self.assertEqual([2, 2, 3, 3, 5, 5], list(p.factorize(900)))
from fractions import Fraction from itertools import combinations from tqdm import tqdm from primes import Prime prime = Prime() prime.sieve(10000000 // 2) #prime.sieve(100000) def product(v): r = 1 for e in v: r *= e return r def φ(n): factors = set(prime.factor(n)) return n * product(f - 1 for f in factors) // product(factors) def is_permutation(a, b): return sorted(str(a)) == sorted(str(b)) def quick_totients(rmax): for r in range(1, rmax): print(r) for factors in combinations(prime.primes(), r):