def test_generate(): from sympy.ntheory.generate import sieve sieve._reset() assert nextprime(-4) == 2 assert nextprime(2) == 3 assert nextprime(5) == 7 assert nextprime(12) == 13 assert prevprime(3) == 2 assert prevprime(7) == 5 assert prevprime(13) == 11 assert prevprime(19) == 17 assert prevprime(20) == 19 sieve.extend_to_no(9) assert sieve._list[-1] == 23 assert sieve._list[-1] < 31 assert 31 in sieve assert nextprime(90) == 97 assert nextprime(10**40) == (10**40 + 121) assert prevprime(97) == 89 assert prevprime(10**40) == (10**40 - 17) assert list(sieve.primerange(10, 1)) == [] assert list(primerange(10, 1)) == [] assert list(primerange(2, 7)) == [2, 3, 5] assert list(primerange(2, 10)) == [2, 3, 5, 7] assert list(primerange(1050, 1100)) == [1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097] s = Sieve() for i in range(30, 2350, 376): for j in range(2, 5096, 1139): A = list(s.primerange(i, i + j)) B = list(primerange(i, i + j)) assert A == B s = Sieve() assert s[10] == 29 assert nextprime(2, 2) == 5 raises(ValueError, lambda: totient(0)) raises(ValueError, lambda: reduced_totient(0)) raises(ValueError, lambda: primorial(0)) assert mr(1, [2]) is False func = lambda i: (i**2 + 1) % 51 assert next(cycle_length(func, 4)) == (6, 2) assert list(cycle_length(func, 4, values=True)) == \ [17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14] assert next(cycle_length(func, 4, nmax=5)) == (5, None) assert list(cycle_length(func, 4, nmax=5, values=True)) == \ [17, 35, 2, 5, 26] sieve.extend(3000) assert nextprime(2968) == 2969 assert prevprime(2930) == 2927 raises(ValueError, lambda: prevprime(1))
def generate_encodings(size: int) -> [int]: """ Generate the encodings for size number of cards. We use prime numbers so when multiplied together, no two cards would generate the encoding of a third card. """ sieve.extend_to_no(size) return list(sieve._list[:size])
def part_two_fn(): b = 105700 c = 122700 ## sieve._reset() sieve.extend_to_no(c) h = 0 for i in range(b, c + 1, 17): if i not in sieve: h += 1 return h
def problem7(nr_of_prime): """Problem 7 - 10001st prime""" # old code :) # primelist = [] # i = 2 # while len(primelist) < nr_of_prime: # prime = True # for x in primelist: # if i % x == 0: # prime = False # break # if prime: # primelist.append(i) # i = i+1 sieve.extend_to_no(nr_of_prime) return sieve._list[nr_of_prime - 1]
""" Find the smallest number n such that the number of divisors of n is equal to (2 ^ 500500) """ import heapq from sympy import sieve sieve.extend_to_no(500500) H = [(3, 3, 1), (4, 2, 3)] i = 3 # next prime to add from sieve (indexing starts at 1 so 3 would mean the 3rd prime (5)) n = 2 # number so far (mod 500500507) for _ in range(2, 500501): p = heapq.heappop(H) if p[2] == 1: heapq.heappush(H, (sieve[i], sieve[i], 1)) i += 1 nextE = (p[2] + 1) * 2 - 1 heapq.heappush(H, (p[1]**(nextE - p[2]), p[1], nextE)) n *= p[0] n %= 500500507 print(n)
def test_generate(): from sympy.ntheory.generate import sieve sieve._reset() assert nextprime(-4) == 2 assert nextprime(2) == 3 assert nextprime(5) == 7 assert nextprime(12) == 13 assert prevprime(3) == 2 assert prevprime(7) == 5 assert prevprime(13) == 11 assert prevprime(19) == 17 assert prevprime(20) == 19 sieve.extend_to_no(9) assert sieve._list[-1] == 23 assert sieve._list[-1] < 31 assert 31 in sieve assert nextprime(90) == 97 assert nextprime(10**40) == (10**40 + 121) assert prevprime(97) == 89 assert prevprime(10**40) == (10**40 - 17) assert list(sieve.primerange(10, 1)) == [] assert list(sieve.primerange(5, 9)) == [5, 7] sieve._reset(prime=True) assert list(sieve.primerange(2, 12)) == [2, 3, 5, 7, 11] assert list(sieve.totientrange(5, 15)) == [4, 2, 6, 4, 6, 4, 10, 4, 12, 6] sieve._reset(totient=True) assert list(sieve.totientrange(3, 13)) == [2, 2, 4, 2, 6, 4, 6, 4, 10, 4] assert list(sieve.totientrange(900, 1000)) == [totient(x) for x in range(900, 1000)] assert list(sieve.totientrange(0, 1)) == [] assert list(sieve.totientrange(1, 2)) == [1] assert list(sieve.mobiusrange(5, 15)) == [-1, 1, -1, 0, 0, 1, -1, 0, -1, 1] sieve._reset(mobius=True) assert list(sieve.mobiusrange(3, 13)) == [-1, 0, -1, 1, -1, 0, 0, 1, -1, 0] assert list(sieve.mobiusrange(1050, 1100)) == [mobius(x) for x in range(1050, 1100)] assert list(sieve.mobiusrange(0, 1)) == [] assert list(sieve.mobiusrange(1, 2)) == [1] assert list(primerange(10, 1)) == [] assert list(primerange(2, 7)) == [2, 3, 5] assert list(primerange(2, 10)) == [2, 3, 5, 7] assert list(primerange(1050, 1100)) == [1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097] s = Sieve() for i in range(30, 2350, 376): for j in range(2, 5096, 1139): A = list(s.primerange(i, i + j)) B = list(primerange(i, i + j)) assert A == B s = Sieve() assert s[10] == 29 assert nextprime(2, 2) == 5 raises(ValueError, lambda: totient(0)) raises(ValueError, lambda: reduced_totient(0)) raises(ValueError, lambda: primorial(0)) assert mr(1, [2]) is False func = lambda i: (i**2 + 1) % 51 assert next(cycle_length(func, 4)) == (6, 2) assert list(cycle_length(func, 4, values=True)) == \ [17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14] assert next(cycle_length(func, 4, nmax=5)) == (5, None) assert list(cycle_length(func, 4, nmax=5, values=True)) == \ [17, 35, 2, 5, 26] sieve.extend(3000) assert nextprime(2968) == 2969 assert prevprime(2930) == 2927 raises(ValueError, lambda: prevprime(1))
from sympy import ntheory from sympy import sieve from sympy import generate sieve._reset( ) # An infinite list of prime numbers, implemented as a dynamically growing sieve of Eratosthenes.#When a lookup is # requested involving an odd number that has not been sieved,the sieve is automatically extended up to that number. print(13 in sieve) # prints 'True' if the input is prime, sieve.extend(40) #for input n grows the sieve to contain all primes <= n print(sieve[10]) # returns n th prime sieve.extend_to_no(15) # extends upto n th prime # mobiusrange(a,b) genarates Mobius numbers for the range [a,b), means output will be a list. Mob_func = sieve.mobiusrange(7, 12) print('\n Mobius function outputs in the given range:', [i for i in Mob_func]) # primerange(a,b) generates all prime numbers in the range [a,b) Prime_list = sieve.primerange(1, 5) print('\n Primes in the given range are :', [i for i in Prime_list]) #search(n) return the indices i, j of the primes that bound n. If n is prime then i == j. Although n can be an expression, # if ceiling cannot convert it to an integer then an error will be raised. x, y = sieve.search(25) print('\n The given input is in between ', '(', x, ',', y, ')', 'th Primes \n') #totientrange(a, b) generates all totient numbers for the range [a, b). Simply it is a list of outputs
#!/usr/bin/env python3 ##### IMPORTS from cryptography.hazmat.primitives import hashes from cryptography.hazmat.backends import default_backend from secrets import randbits import socket from sympy import gcd, isprime, primefactors, sieve # sieve speed-up courtesy: Wiener, Michael J. "Safe Prime Generation with a Combined Sieve." IACR Cryptol. ePrint Arch. 2003 (2003): 186. # create a sieve of values to avoid sieve.extend_to_no(150) # somewhere around here is best for my workstation prime_list = list(sieve._list)[2:] prime_avoid = [(r - 1) // 2 for r in prime_list] ##### METHODS def split_ip_port(string): """Split the given string into an IP address and port number. PARAMETERS ========== string: A string of the form IP:PORT. RETURNS ======= If successful, a tuple of the form (IP,PORT), where IP is a string and PORT is a number. Otherwise, returns None.
from sympy import sieve from sympy import factorint from math import sqrt import cProfile pr = cProfile.Profile() pr.enable() max_n = 20000000 print("Solving factorizations up to n = {}".format(max_n)) print("Calculating all necessary primes...") sieve.extend_to_no(max_n) print("Factoring") for x in range(2, max_n + 1): if x % 100000 == 0: print("Currently at: {}".format(x)) factorint(x) pr.disable() pr.print_stats(sort='time')