# Prime number generatordefprime_generator(end):
for n in range(2, end): # n starts from 2 to endfor x in range(2, n): # check if x can be divided by nif n % x ==0: # if true then n is not primebreakelse: # if x is found after exhausting all values of x
yield n # generate the prime
g =prime_generator(1000) # give firt 1000 prime numbersprint(list(g))
# effiecent and fast way to generate prime numbersdefprimeCheck(n):
if n ==1or n ==0or (n % 2==0and n >2):
returnFalseelse:
for o in range(3, int(n ** (1 / 2)) +1, 2):
if n % o ==0:
returnFalsereturnTruefor a in range(2**15):
ifprimeCheck(a):
prime_numbers.append(a)
Posted by: Guest
on November-28-2020
-1
how to generate prime numbers in a bit range python
defmiller_rabin(n, k):
# Implementation uses the Miller-Rabin Primality Test# The optimal number of rounds for this test is 40# See http://stackoverflow.com/questions/6325576/how-many-iterations-of-rabin-miller-should-i-use-for-cryptographic-safe-primes# for justification# If number is even, it's a composite numberif n ==2or n ==3:
returnTrueif n % 2==0:
returnFalse
r, s =0, n -1while s % 2==0:
r +=1
s //= 2for _ in range(k):
a = random.randrange(2, n -1)
x =pow(a, s, n)
if x ==1or x == n -1:
continuefor _ in range(r -1):
x =pow(x, 2, n)
if x == n -1:
breakelse:
returnFalsereturnTrue"""
a function that uses miller rabin's primality test to genarate a prime number in a certain number of bits length
in other words you give it a number of bits and you will get a prime number with that number of bits
"""
defgenprimeBits(k):
x =""
k =int(k)
for y in range(k):
x = x +"1"
y ="1"for z in range(k-1):
y = y +"0"
x =int(x,2)
y =int(y,2)
p =0whileTrue:
p = random.randrange(y,x)
ifmiller_rabin(p,40):
breakreturn p
Posted by: Guest
on June-04-2020
Code answers related to "generate random prime number python"
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems
resetting your password contact us
Check Your Email and Click on the link sent to your email