The Intrique of Prime Numbers | Detailed Explanation for Java Kids
Prime numbers are tricky to spot.
A number that looks like a prime may in fact be a multiple of a smaller prime number.
In this math programming tutorial for kids, we explore how Java can be used to verify prime numbers efficiently.
So let's see how to know for sure that a number is a prime.
Of-course there is only one way:
Understanding Prime Number Logic in Java
A prime number is one that can only be divided by itself and one (1).
Let's try to draft a Java algorithm that checks prime numbers, with the number 97 in consideration.
To know for sure whether it is a prime number, we will
recursively (repetitively) divide it by every number between
2 and 96 (97 minus 1).
If none of these divisors gives a remainder of zero, then 97
is certainly a prime number.
Create a new Java class file; File, New File.
Call it CheckPrime.
Type out the adjoining Java code for checking prime numbers.
Base Theory of Quick-Check for Primeness in Java
Since the world is always in a hurry, we can make use of a
little extra speed.
This Java code example shows how to check if a number is prime using a fast algorithm based on complementary factors.
Consider the number 36; Its factors are:
1, 2, 3, 4, 6, 9, 12, 18 and 36.
Every factor of 36, when arranged in ascending or descending
order, can be divided into 2 equal parts at the position of its square-root.
1, 2, 3, 4, |, 9, 12, 18, 36
It is easily seen that every factor of 36 on one side of the
divide has a complementary factor on the other side.
Figure: Complementary factors to expedient quick check for prime numbers in Java.
Hence, we can search for only a particular group of
factors, (preferably the more compact group, i.e, between
1 and √36) to see if 36 has any factors.
A fast Check for Primeness in Java
So for our quick prime number check Java algorithm, we will use the range of
2 to √number.
Type out the adjoining Java code for fast check for primeness.
Note: You can simply tweak the existing function from the previous exercise.
You can comment out the Java code for the main class
from the previous lesson if you have been following.
So! Java Fun Practice Exercise - Check Prime Number
As a fun practice exercise, feel free to try out your own numbers,
and see how the Java code checks the numbers to ascertain which ones are prime numbers.
Java Code for Checking Prime Class.
package arithmetic;
public class CheckPrime {
private final intprime_suspect; // We suspect that this number is prime private final doublesquare_root; // this variable is a helping one. private final inttest_range; // range for minimal looping
publicCheckPrime(int val) { // let's see whether prime_suspect is at a premium(is a prime). prime_suspect = val; square_root = Math.sqrt(prime_suspect); // Get square root test_range = (int) Math.ceil(square_root); // Extract an absolute value
}
/**
* Does the actual evaluation to see if our number is prime.
*/ public String verifyPrime() { /* Loop through searching for factors. */ for (int i = 2; i < prime_suspect; i++) { if ((prime_suspect % i) == 0) { return (prime_suspect + " is not a prime number.\n"
+ "At least " + i + " is a factor of " + prime_suspect);
}
}
// If no factor is found: return (prime_suspect + " is a prime number.");
}
}
Java Code for Checking Prime - Main Class.
package arithmetic;
public class Arithmetic {
public static void main(String[] args) {
System.out.println("Welcome to our demonstration sequels");
System.out.println("Hope you enjoy (and follow) the lessons.");
System.out.println("");
/*
* Test for the primeness of a number.
*/
CheckPrime new_test = new CheckPrime(97); // Is 97 is prime number?
System.out.println(new_test.verifyPrime());
System.out.println();
}
}
Java Code for Checking Prime Fast.
package arithmetic;
public class CheckPrimeFast {
private final intprime_suspect; // We suspect that this number is prime private final doublesquare_root; // this variable is a helping one. private final inttest_range; // range for minimal looping
publicCheckPrimeFast(int val) { // let's see whether prime_suspect is at a premium(is a prime). prime_suspect = val; square_root = Math.sqrt(prime_suspect); // Get square root test_range = (int)Math.ceil(square_root); // Extract an absolute value
}
/**
* Verifies primeness quickly.
* @return String value
*/ public String verifyPrimeFast() { /* Loop through a small range searching for factors. */ for (int i = 2; i < test_range; i++) { if ((prime_suspect % i) == 0) { return (prime_suspect + " is not a prime number.\n"
+ "At least " + i + " is a factor of " + prime_suspect);
}
}
// If no factor is found: return (prime_suspect + " is a prime number.");
}
}
Java Code for Checking Prime Fast - Main Class.
package arithmetic;
public class Arithmetic {
public static void main(String[] args) {
System.out.println("Welcome to our demonstration sequels");
System.out.println("Hope you enjoy (and follow) the lessons.");
System.out.println("");
/*
* Test for the primeness of a number.
*/
CheckPrime new_test = new CheckPrime(97); // Is 97 is prime number?
System.out.println(new_test.verifyPrime());