def test_nth_prime(): """Test nth prime function.""" assert primesieve.nth_prime(25) == 97 assert primesieve.nth_prime(26) == 101 assert primesieve.nth_prime(1, 100) == 101 assert primesieve.nth_prime(100, 1000000) == 1001311 assert primesieve.nth_prime(10**8) == 2038074743
def generate_hash(self): c = nth_prime(1000000) a_idx = np.random.randint(10000, 1000000) a = nth_prime(a_idx) b_idx = np.random.randint(10000, 1000000) b = nth_prime(b_idx) return lambda x: (a * x + b) % c % self.input_size
def test_nth_prime(self): for testcase1 in ((25, 97), (26, 101), (10**8, 2038074743)): self.assertEqual(primesieve.nth_prime(testcase1[0]), testcase1[1]) for testcase2 in (((1, 100), 101), ((100, 1000000), 1001311)): self.assertEqual( primesieve.nth_prime(testcase2[0][0], testcase2[0][1]), testcase2[1])
def __init__(self, m, d, n=12): self.m = m self.d = d self.R = [ MultiCongGenerator(nth_prime(randint(1000, 10000000)), 63018038201, nth_prime(randint(10000, 100000))) for _ in range(n) ] self.n = n
def __init__(self, input_size, num_perm=128, num_minhash=1): self.input_size = input_size self.num_perm = num_perm self.num_minhash = num_minhash self.A1s, self.A2s, self.A3s = [], [], [] for _ in range(self.num_minhash): self.A1s.append( torch.randint(0, nth_prime(1000000), torch.Size((1, self.num_perm)))) self.A2s.append( torch.randint(0, nth_prime(1000000), torch.Size((1, self.num_perm)))) self.A3s.append( torch.randint(0, nth_prime(1000000), torch.Size((1, self.num_perm)))) self.A4s = torch.randint(1, nth_prime(1000000), torch.Size((1, self.num_minhash)))
def __init__(self, m): self.m = m # Pick a random integer between m and 2m i = random.randint(m, 2 * m) # Get the nth prime greater than i self.p = primesieve.nth_prime(1, i) # Set a = random int (less than p) (where a != 0) self.a = random.randint(1, self.p - 1) # Set b = random int (less than p) self.b = random.randint(0, self.p - 1)
def __init__(self, input_size, num_perm=128, num_minhash=1): self.input_size = input_size self.num_perm = num_perm self.num_minhash = num_minhash self.A1s, self.A2s, self.A3s, self.Bases = [], [], [], [] self.BIGPRIME = nth_prime(1000000) for _ in range(self.num_minhash): self.A1s.append( torch.randint(1, self.BIGPRIME, torch.Size( (1, self.num_perm)))) self.A2s.append( torch.randint(1, self.BIGPRIME, torch.Size( (1, self.num_perm)))) self.A3s.append( torch.randint(1, self.BIGPRIME, torch.Size( (1, self.num_perm)))) self.Bases.append( torch.randint(0, self.BIGPRIME, torch.Size( (1, self.num_perm)))) self.A4s = torch.randint(1, self.BIGPRIME, torch.Size((1, self.num_minhash))) self.B4 = np.random.randint(0, self.BIGPRIME)
def next_prime(p): print(primesieve.nth_prime(p))
def test_nth_prime(): assert primesieve.nth_prime(25) == 97 assert primesieve.nth_prime(26) == 101
for i in range(z): x = 1 + (i * z) if x % e == 0: d = int(x / e) break print('d =', d) return [e, n], [d, n] def encrypt(message, public_key): encrypt_message = [] for m in message: encrypt_message.append(pow(ord(m), public_key[0]) % public_key[1]) return encrypt_message def decrypt(message, private_key): decrypt_message = '' for m in message: decrypt_message += chr(pow(m, private_key[0]) % private_key[1]) return decrypt_message p = primesieve.nth_prime(random.randint(50, 150)) #457 q = primesieve.nth_prime(random.randint(50, 150)) #1213 public_key, private_key = RSA(p, q) print('public_key', public_key) print('private_key', private_key) msg = encrypt('Hello, world!', public_key) print('Message:', decrypt(msg, private_key))
from math import log from faktorisering import primesbelow from primesieve import nth_prime def ith_fast(n): stop = int(n * (log(n) + log(log(n) - 1 + 1.8 * log(log(n)) / float(log(n))))) sieve = primesbelow(stop) return sieve[n - len(sieve) - 1] if __name__ == "__main__": import timeit limit = 10001 print ith_fast(limit) print nth_prime(limit) times = 50 t1 = timeit.timeit("ith_fast(1000001)", setup="from __main__ import ith_fast", number=times) / float(times) t2 = timeit.timeit("nth_prime(1000001)", setup="from __main__ import nth_prime", number=times) / float(times) print "My solution used: ", 1000 * t1, "ms" print "The C++ solution used: ", 1000 * t2, "ms" print "The C++ solution was: ", t1 / float(t2), "times faster than mine."
def main(): ''' Driver function ''' print(nth_prime(10001))
import primesieve import random class User: def __init__(self, secret_num, p, g): self.p = p self.g = g self.secret_num = secret_num self.key = pow(g, secret_num) % p def get_key(self): return self.key def get_secret_key(self, companion_key): return pow(companion_key, self.secret_num) % self.p g = primesieve.nth_prime(random.randint(50, 150)) #257 p = primesieve.nth_prime(random.randint(50, 150)) #23 Naruto = User(random.randint(1, 500), p, g) Sasuke = User(random.randint(1, 500), p, g) print('Natuto key:', Naruto.get_secret_key(Sasuke.get_key())) print('Sasuke key:', Sasuke.get_secret_key(Naruto.get_key()))
else: print('\nМ_client != М_server') return -1 R_server = hash_generation(A, М_client, K_server) R_client = hash_generation(A, М_client, K_client) if R_server == R_client: print('R_server == R_client') print(R_server, R_client) return 1 else: print('R_server != R_client') return -1 N = primesieve.nth_prime(random.randint(10, 100)) while True: if prime_check((N - 1) // 2): break N = primesieve.nth_prime(random.randint(10, 100)) print('N =', N) g = generator(N) print('g =', g) k = hash_generation(N, g) print('k =', k) I = 'Username' p = '123456789' s = rand_generation(64)
def test_nth_prime(): assert primesieve.nth_prime(25) == 97 assert primesieve.nth_prime(26) == 101 assert primesieve.nth_prime(1, 100) == 101 assert primesieve.nth_prime(100, 1000000) == 1001311 assert primesieve.nth_prime(10**8) == 2038074743
def test_nth_prime(): assert primesieve.nth_prime(25) == 97 assert primesieve.nth_prime(26) == 101 assert primesieve.nth_prime(1, 100) == 101 assert primesieve.nth_prime(100, 1000000) == 1001311
def __init__(self, a, b): self.a = a self.b = b self.R = MultiCongGenerator(nth_prime(randint(1000, 10000000)), 63018038201, nth_prime(randint(10000, 100000)))
def test_nth_prime(): assert primesieve.nth_prime(25) == 97 assert primesieve.nth_prime(26) == 101
def list_primes(n): for i in range(n): print(primesieve.nth_prime(i))
def __init__(self, N, p = None): self.N = N if p is None: p = nth_prime(1, 1 << max(32, ceil(log2(N)))) assert p >= N, 'Prime number p should be at least N!' self.p = p
def __init__(self, bins, elements): self.bins = bins # range self.elements = elements # domain self.p = nth_prime(1, elements) # finds the first prime greater than `elements`