find 200 digit prime numbers using BigInteger

steve moretz :

One way to do this which absolutely works is starting from 0 up to where you'll find 200 digit prime numbers.In order to do that I wrote this method:

var primeList = arrayListOf(BigInteger("2"))
fun findNextPrime(num : BigInteger): BigInteger {
    val n = num + BigInteger.ONE
    val sqrt = sqrt(num)
    for (bigInteger in primeList) {
        if(bigInteger > sqrt){
            return n
        }
        if(n % bigInteger == BigInteger.ZERO){
            return findNextPrime(num + BigInteger.ONE)
        }
    }
    return n;
}

I add the numbers I find to primeList and check only the numbers less than squareRoot.Even though this was the fastest algorithm I could write but it takes so long after finding a million digits.which is only 7 digits.I'll probably die till it goes to 200 digits.(even though my laptop is i7 8th generation).So the next thing I used was this:

n = 2 * 3 * 5 *... + 1

well n is prime and it's pretty fast to use this method to get to high digits,but there's nothing for sure to get to 200 digits exactly.I got to 198 and 201 digits.But no 200 the code is simple but I post it anyway:

var all = BigInteger.ONE
primeList.forEach {
   all *= it
}
all++
println(all.toString().length)
John Coleman :

It isn't true that 1 + the product of the first n primes is always prime. You are possibly misremebering its role in the proof that there are infinitely many primes. It is true that if p_1, p_2, ..., p_n are the first n primes then

p_1 * p_2 * ... * p_n + 1

is either prime or contains a prime factor which is larger than any of those p_i, but this is consistent with the number being composite. See the Wikipedia article on primorials for more information.

In the case of trying for 200 digits, the product of the first 92 primes + 1 has 199 digits and the product of the first 93 primes + 1 has 201 digits. In both cases, the Miller-Rabin test shows that they are composite. I haven't been able to factor the 199 digit one, but the 201 digit one factors as

509558935064289364432032169616857776489168568369134671296055828054188240764364761921821351373922822013621199759688858354748131233614846920025560717744496960296617420071391914813530238313960697008021211 = 11587 * 43976778723076669062918112506848863078378231498156094873224806080451216083918595142989673890905568483094951217717170825472351016968572272376418461874902646094469441621765074205016849772500275913353

With numbers of this magnitude, the only effective way to get a prime is to randomly generate a candidate number of the target size and test it for primality (using something like the Miller-Rabin test). By the prime number theorem, 200 digit primes are relatively plentiful, so in practice you can find such a prime very rapidly. For example, a Python script I wrote using Miller-Rabin spit out the following 200 digit prime in less than a second:

49675218696612399034240799519655205503986657506787162015105425670413948962864456158664793804627084299081036134562339483478437262146378569515417671690110863951848724044479367633926630234074394356492223

On Edit: Here is the Python script that I used to find a 200 digit prime. The code was for a class I was teaching in cryptography, so I wrote it to be easy to discuss rather than concise or efficient:

import random

#The following function finds s and d in
#n-1 = 2^s*d with d odd
def findSD(n):
    s = 0
    d = n-1
    while d % 2 == 0:
        s = s + 1
        d = d//2
    return s,d

def checkBase(a,n):
    s,d = findSD(n)
    x = pow(a,d,n)
    if x == 1 or x == n-1:
        return "probable prime"
    else:
        for i in range(s-1):
            x = pow(x,2,n)
            if x == 1:
                return "composite"
            elif x == n-1:
                return "probable prime"
        #if you get to this stage, -1 not reached despite s-1
        #squarings -- so must be composite
        return "composite"

def MSR(n,k):
    #Implements the Miller-Selfridge-Rabin test for primality
    for i in range(k):
        a = random.randint(2,n-2)
        if checkBase(a,n) == "composite":
            return "composite"
    #if you get here n has survived k potential witnesses, so
    return "probable prime"

#The following function is slightly different from the one discussed in class:

def prime(n):
    smallPrimes = [2,3,5,7,11,13,17,19]

    for p in smallPrimes:
        if n == p:
            return True
        elif n % p == 0:
            return False

    if MSR(n,20) == "composite":
        return False
    else:
        return True

def findPrime(maxN):
    while True:
        m = random.randint(1,maxN//2)
        n = 2*m+1
        if prime(n):
            return n

For example, findPrime(10**200) will usually give you a 200-digit prime number (though getting a 199 digit or even smaller is possible).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=115893&siteId=1