javascript

generatePrimeNumbersRange()

Parameters: start(int), end(int)

Start and end of the range for prime numbers

Returns: Array of prime numbers in the given range

This function generates a sequence of prime numbers between two given numbers. Prime numbers are numbers that have only two positive divisors: 1 and itself.

Loops
Conditionals
Integer division
Boolean
Functions
Arrays
Medium dificulty

Generating Prime Numbers within a Range in JavaScript

Hello dear programmer! We're about to venture on a fascinating journey of creating a unique Javascript function that returns prime numbers within a specific range. This function will be a handy addition to your toolbox when dealing with number theory problems or simply when you need to spice up your code. Let's get started and thread into the captivating world of prime numbers together!

Step 1: Understanding Prime Numbers in the Context of Programming

Before we can begin writing our function, we need to understand what a prime number is. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In the context of programming, we can determine if a number is prime by checking if it has any divisors other than 1 and itself. If it does, it's not a prime number.

Step 2: Creating the Basic Structure of the Function

Let's start by creating the basic structure of our function. We're going to build a function named generatePrimeNumbersRange which will accept two integers as arguments: start and end. These arguments symbolize the range within which we want to find all prime numbers.

function generatePrimeNumbersRange(start, end) {
  // function body to be filled later
}

Step 3: Adding Prime Check Logic

We now need to create the logic that will check if a number is prime or not. We'll do this by creating a loop that starts from the number 2 and goes up to the square root of the number we're checking. Why the square root? It's because, after the square root, factors start repeating.

function generatePrimeNumbersRange(start, end) {
  for(let i = start; i <= end; i++) {
    let isPrime = true;

    for(let j = 2, sqrt = Math.sqrt(i); j <= sqrt; j++) {
      if(i % j === 0) {
        isPrime = false;
        break;
      }
    }
  }
}

Notice that we initialized isPrime as true for each number in the range, and it will only be set to false if a number other than 1 and the number itself is found which can divide the number.

Step 4: Recording and Returning all Prime Numbers

The last part of our function is to record all prime numbers we find and return that list. We add a condition within the first loop that if isPrime is true after the second loop has finished, it indicates that i is a prime number.

function generatePrimeNumbersRange(start, end) {
  const primes = [];

  for(let i = start; i <= end; i++) {
    let isPrime = true;

    for(let j = 2, sqrt = Math.sqrt(i); j <= sqrt; j++) {
      if(i % j === 0) {
        isPrime = false;
        break;
      }
    }

    if(isPrime && i > 1) primes.push(i);
  }

  return primes;
}

Step 5: Conclusion and Full Code

There you have it! You have built a function capable of generating all prime numbers in a given range. All that's left to do is to call this function with a specific range and see what it returns.

function generatePrimeNumbersRange(start, end) {
  const primes = [];

  for(let i = start; i <= end; i++) {
    let isPrime = true;

    for(let j = 2, sqrt = Math.sqrt(i); j <= sqrt; j++) {
      if(i % j === 0) {
        isPrime = false;
        break;
      }
    }

    if(isPrime && i > 1) primes.push(i);
  }

  return primes;
}

console.log(generatePrimeNumbersRange(10, 50)); // [ 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ]

Therefore, you can see that between 10 and 50, the prime numbers are [ 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 ].

Learn function in:

Prime Numbers Generation

Generating all prime numbers in a given range

Learn more

Mathematical principle

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. To find prime numbers within a range, we iterate over all numbers in the range and check if each is prime. A prime check can be done by checking divisibility up to the square root of the number.

Learn more