![]() |
VOOZH | about |
Many programming contest problems are somehow related to prime numbers. Either we are required to check Prime Numbers, or we are asked to perform certain functions for all prime numbers between 1 to N. Example: Calculate the sum of all prime numbers between 1 and 1000000. Java provides two functions under java.math.BigInteger to deal with Prime Numbers.
A method in the BigInteger class to check if a given number is prime. For certainty = 1, it returns true if BigInteger is prime and false if BigInteger is composite. Below is a Java program to demonstrate the above function.
true
Another method present in BigInteger class. This functions returns the next Prime Number greater than current BigInteger. Below is Java program to demonstrate above function.
17