Prime numbers play a vital role in various applications, from cryptography to data security. Understanding how to work with prime numbers in Java can unlock a world of possibilities. In this article, we will dive deep into the tips and tricks of unleashing the power of prime numbers in Java.
Whether you are a beginner or an experienced developer, this article will provide you with valuable insights and techniques to optimize your code and make it more efficient. We will explore different algorithms and approaches to generate prime numbers, check for primality, and perform various operations on prime numbers.
Through practical examples and step-by-step explanations, you will gain a solid understanding of prime numbers in Java and how to leverage them to solve real-world problems. So, if you are ready to elevate your Java programming skills and harness the power of prime numbers, let’s dive in!
A prime number is an integer greater than 1 and divisible by only 1 and itself. You can also understand it as a number that has exactly two divisors. The first few prime numbers are 2, 3, 5, 7, 11 and so on.
How do you find prime numbers in Java?
You check for the given number if there are other divisors except 1 and itself. First, let’s write the algorithm of the program.
- Step 1: Take a number as input.
- Step 2: Check if there is a divisor of the number in the range of [two, number/2]. It starts with 2 because two is the smallest prime number. It stops in number/2 because if there is no divisor up to this number, there will be no further.
- Step 3: If any divisor is found, the number is not prime; otherwise, the given number is prime.
Prime Number Checker in Java
Now, let’s implement this algorithm in the Java program.
The first step is to write a method, which will get a number n as input and will do all the calculations. This method will show a message in Console, if the number is prime or not, so the returning type will be void.
static void checkPrime(int n)
We will initialize some values such as
int i = 0;
boolean prime = true;
int m = n/2;
We don’t consider numbers below 2. So if(n <= 1) we will display a message in the console, otherwise we will continue the algorithm.
if(n <= 1){
System.out.println("Let's not consider numbers below 2");
}
Next step, we will use a for loop to start from 2 to m and to check if the number n is divided by the counter, so the rest of the division is zero. If it’s zero, that means we found a number that n is divisor, so n is not a prime number.
for(i = 2; i<=m; i++){
if(n%i==0){
System.out.println(n+" is not prime number");
prime = false;
break;
}
}
if(prime == true) {
System.out.println(n+" is prime number");
}
The method will show a message in the Console, whether the number is prime or not. In cases when the number is smaller than 2, will notify the user we don’t consider it.
The final version of the program looks like this:
package JavaCodes;
public class CheckPrimeNumberExample{
static void checkPrime(int n){
int i;
boolean prime=true;
int m=n/2;
if(n <= 1){
System.out.println("Not checking numbers below 2");
}
else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
prime = false;
break;
}
}
if(prime == true) {
System.out.println(n+" is prime number");
}
}
}
public static void main(String args[]){
checkPrime(-1);
checkPrime(3);
checkPrime(17);
checkPrime(20);
}
}
In the Console is displayed this output:
Not checking numbers below 2
3 is prime number
17 is prime number
20 is not prime number
Hoping this was helpful. Keep coding…
For more Java Applications Tutorials, visit Code in Java