1...Prime numbers are only divisible by itsself and 1.
2...Interest in primes dates back to the time of ancient Egyptians.
3...There are many types of primes (Mersenne primes, Fermat primes, and twin primes) to name a few.
4...The largest prime found at the time of this tutoria has 12,978,189 digits. Ya, that's BIG!
5...Internet encryption is based off of factoring large primes.
6...The largest number I've proved prime is "10000000000000000000000013" and it took me 11 days. My latest most efficient version will cut that time by 75%.
7...There are some probabilistic algorithms that can determine to a very high degree weather a number is prime. These algorithms can test very large numbers very quickly, but what they can't do is determine their factors.
8...Most large primes found these days are by super computers or distributed computing.
OK, enough with that. The program I've included is a very simple version of my prime checker. This version is not very efficient, but is intended to demonstrate how to use the java BigInteger. The reason I've used the big integer is because the Java int is very limited in size when trying to find large primes. I've used the number "2147483648" because it is one larger than the upper limit for the "int". The BigInteger stores the number as a String, and therefore can be very large.
Check it out, and let me know if you have questions.
/* Author: TA Lucas
* Purpose: Will find the next prime number >= checkThisNumber
*/
import java.math.BigInteger;
public class Nextprime
{
public static void main(String[]args)
{
BigInteger checkThisNumber = new BigInteger("2147483648");
// This counts upwards until it reaches the next prime...
// Includes the current number.
while(!isPrime(checkThisNumber))
checkThisNumber = checkThisNumber.add(new BigInteger("1"));
// Prints the next prime number to the screen
System.out.println(checkThisNumber.toString());
}
// This method tests whether a given number is prime or not. It is not the most efficient
// It does a couple checks to eliminate several numbers that cannot be prime. For example,
// it eliminates all even numbers except 'two'. It also only check numbers that are
// less than the square root of the number passed in....the upper limit is lowered each time
// by the divisor.
public static boolean isPrime ( BigInteger num )
{
boolean prime = true;
BigInteger upperLimit = num;
BigInteger divisor = new BigInteger("3");
// Two is the only odd prime, so it returns true.
if(num.compareTo(new BigInteger("2")) == 0)
{
prime = true;
return prime;
}
// If any number is divisible by two it is not prime. This eliminates
// 50% of the numbers we have to check.
if (num.mod(new BigInteger("2")).compareTo(new BigInteger("0")) == 0)
{
prime = false;
return prime;
}
while(upperLimit.compareTo(divisor) >= 0 && prime)
{
// If there is no remainder it is not prime.
if (num.mod(divisor).compareTo(new BigInteger("0")) == 0)
{
prime = false;
break;
}
upperLimit = num.divide(divisor);
// Adds 2 to skip even numbers.
divisor = divisor.add(new BigInteger("2"));
}
return prime;
}
}


Sign In
Create Account


Back to top









