The Intrique of Prime Numbers | Detailed Explanation for JavaScript 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 JavaScript 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 JavaScript
A prime number is one that can only be divided by itself and one (1).
Let's try to draft a JavaScript 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 file; On Notepad++: File, New.
Call it CheckPrime.html.
Type out the adjoining JavaScript code for checking for primeness.
Base Theory of Quick-Check for Primeness in JavaScript
Since the world is always in a hurry, we can make use of a
little extra speed.
This JavaScript 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:
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.
It is easily seen that every factor of 36 on one side of the divide has a complementary factor on the other side.
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.
Fast Check for Primeness in JavaScript
So for our quick prime number check JavaScript algorithm, we will use the range of
2 to √number.
Type out the adjoining JavaScript code for fast prime number check.
So! JavaScript Fun Practice Exercise - Check Prime Number
As a fun practice exercise, feel free to try out your own numbers, and see how the JavaScript code checks the numbers to ascertain which ones are prime numbers.
JavaScript Code for Checking Prime.
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Testing For A Prime Number</title>
</head>
<body>
<h4>The number 97 seems like a prime number. Let's find out.</h4>
<div id="test_result"></div>
<script>
var prime_suspect = 97; // We are suspecting this to be a prime number
var failed_test = false; // We will change it to 'true' if 'prime_suspect' fails our test
/* The easy way out is to loop numbers 2 - 96 and see if they can divide
97 without without remainder. */
for (var i = 2; i < prime_suspect; i++) {
if ((prime_suspect % i) == 0) { // %(modulus) is explained below.
failed_test = true;
document.getElementById("test_result").innerHTML =
"No, " + prime_suspect + " is not a prime number. The number " + i + " is a factor of " + prime_suspect;
break; // Abandon the rest of the loop. 'prime_suspect' has failed our test.
}
}
if (failed_test == false) { // see if prime_suspect passed the test
document.getElementById("test_result").innerHTML = "Yes, " + prime_suspect + " is a prime number.";
}
</script>
</body>
</html>
JavaScript Code for Checking Prime Fast
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Testing For A Prime Number</title>
</head>
<body>
<h4>The number 91 seems like a prime number. Let's find out.</h4>
<div id="test_result"></div>
<script>
var prime_suspect = 91; // We are suspecting this to be a prime number
var test_range = Math.sqrt(prime_suspect); // square root
test_range = Math.abs(test_range); // test_range may have come out as a decimal number, make it absolute.
var failed_test = false; // We will change it to 'true' if 'prime_suspect' fails our test
/* Check in the range 2 - √97 instead. */
for (var i = 2; i <= test_range; i++) {
if ((prime_suspect % i) == 0) { // modulus(%) is explained below.
failed_test = true;
document.getElementById("test_result").innerHTML =
"No, " + prime_suspect + " is not a prime number. The number " + i + " is a factor of " + prime_suspect;
break; // Abandon the rest of the loop. 'prime_suspect' has failed our test.
}
}
if (failed_test == false) { // see if prime_suspect passed the test
document.getElementById("test_result").innerHTML = "Yes, " + prime_suspect + " is a prime number.";
}
</script>
</body>
</html>