generate random prime number python

Solutions on MaxInterview for generate random prime number python by the best coders in the world

showing results for - "generate random prime number python"
Edoardo
19 Oct 2019
1# Prime number generator
2def prime_generator(end):
3    for n in range(2, end):     # n starts from 2 to end
4        for x in range(2, n):   # check if x can be divided by n
5            if n % x == 0:      # if true then n is not prime
6                break
7        else:                   # if x is found after exhausting all values of x
8            yield n             # generate the prime
9
10
11g = prime_generator(1000)       # give firt 1000 prime numbers
12print(list(g)) 
Grégoire
27 Sep 2018
1import sympy
2
3primeNumber = sympy.randprime(min, max)
4
Luis
13 Jan 2017
1# effiecent and fast way to generate prime numbers
2def primeCheck(n):
3    if n == 1 or n == 0 or (n % 2 == 0 and n > 2):
4        return False
5    else:
6        for o in range(3, int(n ** (1 / 2)) + 1, 2):
7            if n % o == 0:
8                return False
9        return True
10
11
12for a in range(2**15):
13    if primeCheck(a):
14        prime_numbers.append(a)
Janelle
24 May 2020
1def miller_rabin(n, k):
2
3    # Implementation uses the Miller-Rabin Primality Test
4    # The optimal number of rounds for this test is 40
5    # See http://stackoverflow.com/questions/6325576/how-many-iterations-of-rabin-miller-should-i-use-for-cryptographic-safe-primes
6    # for justification
7
8    # If number is even, it's a composite number
9
10    if n == 2 or n == 3:
11        return True
12
13    if n % 2 == 0:
14        return False
15
16    r, s = 0, n - 1
17    while s % 2 == 0:
18        r += 1
19        s //= 2
20    for _ in range(k):
21        a = random.randrange(2, n - 1)
22        x = pow(a, s, n)
23        if x == 1 or x == n - 1:
24            continue
25        for _ in range(r - 1):
26            x = pow(x, 2, n)
27            if x == n - 1:
28                break
29        else:
30            return False
31    return True
32
33"""
34a function that uses miller rabin's primality test to genarate a prime number in a certain number of bits length
35in other words you give it a number of bits and you will get a prime number with that number of bits
36"""
37def genprimeBits(k):
38    x = ""
39    k = int(k)
40    for y in range(k):
41        x = x + "1"
42    y = "1"
43    for z in range(k-1):
44        y = y + "0"
45    x = int(x,2)
46    y = int(y,2)
47    p = 0
48    while True:
49        p = random.randrange(y,x)
50        if miller_rabin(p,40):
51            break
52    return p
queries leading to this page
prime number in python without for elserandom prime number generator pythonprime generator python prime number generation in pythonhow to generate prime numbers in pythonprimes generator python lineargenerate a random prime number pythonsequence of prime numbers in python with generatorrandom prime number generate in pythonpython generate primesgenerate random prime pythona function that yields prime number when requested pythonpython generate n bit primepython prime number listhow to make a prime number generator in pythonpython prime number generatorgenerate random prime number pythonhow to make a prime number genarator using python consolepython generate random prime numberpython class prime generatorpython prime number generator functionprime number generator in pythonpython generator primesprime number generate pythonpython prime generator 5chow to generate random prime numbers in pythonfastest way to generate prime numbers pythonpython generate random giftcard numbersnext prime generator pythonrandom prime numbers pythonpython calculate online prime numbersprime numbers generator pythonprime numbers algorithm pythonrandom prime generator pythonpython generate primeprime generator in pythonpython prime numbers generatorrandom prime number pythonfunction for prime numbers in pythonrandom prime number generator in pythonpython prime generatorgenerating prime numbers in pythonhow to generate prime numbers using generatoras in pythonpython get primeshow to generate prime numbers in a bit range pythongenerate radndom prime number in pythonprime numbers till n using generators in pythonpython generate random prime digitshow to generate a random prime number in pythonpython random prime numberprime number generator python 3generate prime numbers in pythongenerate prime pythonpython prime generator number callprime numbers range using pythonpython prime number generatorpython generage prime numbersprime1 prime generator in pythonpython random prime number generatorrandom prime pythonpython generate prime numberspython generate prime numberpython random primegenerate a prime number pythonpython primes generatorgenerate prime number in pythonpython random prime numbnerexpensive function python prime numberssgenerate random prime number in pythonpython generator for infinite prime numbersgenerate prime numbers pythonrandom prime number crypto pythongenerate fake prime numbers in pythonpython prime generator codingpython generator prime numbersprime numbers python generatorprimes generator pythonprime numbers pythonpython efficient prime number generatorpython generator for prime numbersprime number generator pythongenerate random prime number python