usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

How to Code Prime Number Algorithms in JavaScript - Fun Students Activity



Randomness of Prime Numbers | Maths Explanation for JavaScript Kids

Prime numbers are natural numbers greater than 1 with no positive divisors other than 1 and itself. Prime numbers, in ascending order, are:

2, 3, 5, 7, 11, 13, 17, 19, 23, ...

Because prime numbers are so random in their progression, they are very difficult to predict as they get larger.
For the reason of their unpredictability, prime number are applied in

  • Cryptography: RSA encryption relies on large prime numbers.
  • Hashing: Prime numbers help reduce collisions in hash functions.
  • Data structures: Primes are used in sizing hash tables and optimizing algorithms.

In this beginner-friendly Maths JavaScript tutorial for kids, we'll show how to list prime numbers in a fun and interactive way for STEM students.
Writing a JavaScript code to list prime numbers will involve checking every number in a range of interest and gathering those that are without factors.


Logic for Determining Prime Numbers | Detailed Explanation for JavaScript Kids

Say we want to implement a JavaScript algorithm to list all prime numbers between 2 and 100 inclusive, we would start from 2; check to see whether it has any factors; keep it as a prime number if it has no factors; otherwise discard it.
We would then go to 3, and repeat the same process.
Repeat same for 4, 5, 6, 7, ..., 98, 99, 100.

But we can always use a few tricks for the JavaScript algorithm...

We will take the following steps:
Step 1:

First, we'll start our range from 9 (keeping 2, 3, 5, and 7 as prime numbers).

Step 2:

Next, we'll only check through odd numbers.

Step 3:

Next, we'll check against the factors 2, 3, and 5.

Step 4:

Lastly, we'll check with a subset of smaller prime numbers that we'll gather as our check progresses.

Create 2 new files; On Notepad++: File, New.
Call them PrimeNumbers.html and PrimeNumbers.js.
Type out the adjoining JavaScript code that lists prime numbers.


So! JavaScript Fun Practice Exercise - List Prime Numbers

As a fun practice exercise, feel free to try out your own boundary values, and see how the JavaScript code lists the prime numbers between those boundary values.







JavaScript Code for PrimeNumbers.html

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Listing Prime Numbers</title>
        <script src="PrimeNumbers.js"></script>
    </head>
    <body>
        <h4>
            Prime numbers are numbers that can only be divided by themselves and 1 
            without remainder.<br />Here is a list of them (between 1 and 99 inclusive).
        </h4>

        <div id="my_primes"></div>

        <script>
            /*
            * The array variable prime_numbers has our prime numbers.
            * Use the array method 'join' to display the values. 
            */

            document.getElementById("my_primes").innerHTML = prime_numbers.join(", ");
        </script>
    </body>
</html>

JavaScript Code for PrimeNumbers.js

var start = 1
var end = 99;
var progress = 9;
var do_next_iteration = false;
var prime_numbers = []// This array variable would hold our prime numbers as they get verified.

/*
 * STEP 1: 
 */

prime_numbers[0] = 2;
prime_numbers[1] = 3;
prime_numbers[2] = 5;
prime_numbers[3] = 7;
var square_root = 0;
var index = 0// index into our array
var current = 3// Points to the current occupied array position.

/*
 * STEP 2:
 */

for (; progress < end; progress += 2) {

    do_next_iteration = false// a flag

    //STEPS 3, 4:
    /*
     * Alright, one last check.
     * See to it that no prime number less than variable progress is a factor. 
     */

    square_root = Math.abs(Math.sqrt(progress));

    index = 0;
    for (; prime_numbers[index] <= square_root; index++) {
        if ((progress % prime_numbers[index]) == 0) {
            do_next_iteration = true;
            break;
        }
    }
    if (do_next_iteration) {
        continue;
    }

    /* 
     * If 'progress' cleared all those hurdles, then it is definitely a prime number. 
     * We'll also get our subset of smaller prime numbers from here.
     */

    prime_numbers[++current] = progress;
}




<< PreviousNext >>