Programming Logic to Check and Generate Prime numbers

How to check a number is a Prime number?

What is a Prime number?

A prime number is a positive integer greater than 1 whose only factors are 1 and itself, or you can say that it is found only in the 1’s table and in its own table.

For Example:

  • 2 is a prime number because it is found only in the 1’s table and its own table.
  • 3 is also a prime number because it follows the same rule as 2.
  • 4 is not a prime number because it is found in the 1’s and its own table as well as in the table of others (4 is found in 2’s table, 2 x 2 = 4).

Programming Logic to check Prime number

Implementing the above prime number logic in Python, PHP, and JavaScript.

Note: Prime numbers are infinite, and you should be aware that the following code uses loops to check for primes, so big integers can take time or cause your server to crash.

from math import sqrt

theNum = 121
divisibleBy = None

def isPrime(num):
    numSqrt = sqrt(num)
    global divisibleBy
    i = 2
    while i <= numSqrt:
        if(num % i == 0):
            divisibleBy = i
            return False
        i += 1
    return num > 1

if(theNum <= 1):
    print('Please enter a Valid number (Number must be Greater than 1).')
elif(isPrime(theNum)):
    print(f'{theNum} is a Prime Number.')
else:
    print(f'{theNum} is Not a Prime Number, It is divisible by {divisibleBy}.')
121 is Not a Prime Number, It is divisible by 11.

<?php
$theNum = 29;
$divisibleBy = null;

function isPrime($num)
{
    global $divisibleBy;
    for ($i = 2, $s = sqrt($num); $i <= $s; $i++) {
        if ($num % $i == 0) {
            $divisibleBy = $i;
            return false;
        }
    }
    return $num > 1;
}

if ($theNum <= 1) {
    echo "Please enter a Valid number (Number must be Greater than 1).";
} elseif (isPrime($theNum)) {
    echo "$theNum is a Prime Number.";
} else {
    echo "$theNum is Not a Prime Number, It is divisible by $divisibleBy.";
}
29 is a Prime Number.

const theNum = 173;
let divisibleBy = null;

const isPrime = (num) => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i++)
    if (num % i === 0) {
      divisibleBy = i;
      return false;
    }
  return num > 1;
};

if (theNum <= 1) {
  console.log("Please enter a Valid number (Number must be Greater than 1).");
} else if (isPrime(theNum)) {
  console.log(`${theNum} is a Prime Number.`);
} else {
  console.log(
    `${theNum} is Not a Prime Number, It is divisible by ${divisibleBy}.`
  );
}
173 is a Prime Number.

Logic to generate prime numbers between two numbers

from math import sqrt

def isPrime(num):
    numSqrt = sqrt(num)
    i = 2
    while i <= numSqrt:
        if(num % i == 0):
            return False
        i += 1
    return num > 1

def primeGenerator(start, howMany=10):
    primeList = []
    i = start
    while len(primeList) < howMany:
        if(isPrime(i)):
            primeList.append(i)
        i += 1
    return primeList

print(primeGenerator(500, 5))
[503, 509, 521, 523, 541]

function isPrime($num)
{
    for ($i = 2, $s = sqrt($num); $i <= $s; $i++)
        if ($num % $i == 0) return false;
    return $num > 1;
}

function primeGenerator($start, $howMany = 10)
{
    $primeList = [];
    $i = $start;
    while(count($primeList) < $howMany){
        if(isPrime($i)) array_push($primeList,$i);
        $i++;
    }
    return $primeList;
}

print_r(primeGenerator(150));
Array
(
    [0] => 151
    [1] => 157
    [2] => 163
    [3] => 167
    [4] => 173
    [5] => 179
    [6] => 181
    [7] => 191
    [8] => 193
    [9] => 197
)

const isPrime = (num) => {
  for (let i = 2, s = Math.sqrt(num); i <= s; i++)
    if (num % i === 0) return false;
  return num > 1;
};

const primeGenerator = (start, howMany = 10) => {
  const primeList = []
  let i = start
  while (primeList.length < howMany){
    if(isPrime(i))
 primeList.push(i);
    i++;
  }
  return primeList;
}

console.log(primeGenerator(29));
[
  29, 31, 37, 41, 43,
  47, 53, 59, 61, 67,
  71
]