Skip to content
Related Articles

Related Articles

Improve Article

Prime Numbers

  • Difficulty Level : Easy
Geek Week

A prime number is a natural number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 …..

Some interesting fact about Prime numbers 

  • Two is the only even Prime number.
  • Every prime number can be represented in form of 6n+1 or 6n-1 except the prime number 2 and 3, where n is a natural number.
  • Two and Three are only two consecutive natural numbers that are prime.
  • Goldbach Conjecture: Every even integer greater than 2 can be expressed as the sum of two primes.
  • Wilson Theorem: Wilson’s theorem states that a natural number p > 1 is a prime number if and only if
(p - 1) ! ≡  -1   mod p 
OR  (p - 1) ! ≡  (p-1) mod p
an-1 ≡ 1 (mod n)
OR 
an-1 % n = 1
  • Prime Number Theorem: The probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or to the logarithm of n.
  • Lemoine’s Conjecture: Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine’s conjecture.

How we check whether a number is Prime or not? 

  • Naive solution
    A naive solution is to iterate through all numbers from 2 to sqrt(n) and for every number check if it divides n. If we find any number that divides, we return false.

C++




// A school method based C++ program to
// check if a number is prime
#include <bits/stdc++.h>
using namespace std;
 
// function check whether a number
// is prime or not
bool isPrime(int n)
{
    // Corner case
    if (n <= 1)
        return false;
 
    // Check from 2 to square root of n
    for (int i = 2; i <= sqrt(n); i++)
        if (n % i == 0)
            return false;
 
    return true;
}
 
// Driver Code
int main()
{
    isPrime(11) ? cout << " true\n" : cout << " false\n";
    return 0;
}

Java




// A school method based Java program to
// check if a number is prime
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Check for number prime or not
    static boolean isPrime(int n)
    {
 
        // Check if number is less than
        // equal to 1
        if (n <= 1)
            return false;
 
        // Check if number is 2
        else if (n == 2)
            return true;
 
        // Check if n is a multiple of 2
        else if (n % 2 == 0)
            return false;
 
        // If not, then just check the odds
        for (int i = 3; i <= Math.sqrt(n); i += 2)
        {
            if (n % i == 0)
                return false;
        }
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        if (isPrime(19))
            System.out.println("true");
 
        else
            System.out.println("false");
    }
}
 
// This code is contributed by Ronak Bhensdadia

Python3




# A school method based Python3 program
# to check if a number is prime
 
# function check whether a number
# is prime or not
 
#import sqrt from math module
from math import sqrt
 
def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to sqrt(n)
    for i in range(2, int(sqrt(n))+1):
        if (n % i == 0):
            return False
 
    return True
 
 
# Driver Code
if isPrime(11):
    print("true")
else:
    print("false")
 
# This code is contributed by Sachin Bisht

C#




// A school method based C# program to
// check if a number is prime
using System;
 
class GFG {
    // function check whether a
    // number is prime or not
    static bool isPrime(int n)
    {
        // Corner case
        if (n <= 1)
            return false;
 
        // Check from 2 to n-1
        for (int i = 2; i < n; i++)
            if (n % i == 0)
                return false;
 
        return true;
    }
 
    // Driver Code
    static void Main()
    {
        if (isPrime(11))
            Console.Write(" true");
 
        else
            Console.Write(" false");
    }
}
 
// This code is contributed by Sam007

PHP




<?php
// A school method based PHP program to
// check if a number is prime
 
// function check whether a number
// is prime or not
function isPrime($n)
{
    // Corner case
    if ($n <= 1)
        return false;
 
    // Check from 2 to n-1
    for ($i = 2; $i < $n; $i++)
        if ($n % $i == 0)
            return false;
 
    return true;
}
 
// Driver Code
if(isPrime(11))
    echo("true");
else
    echo("false");
 
// This code is contributed by Ajit.
?>

Javascript




<script>
 
// A school method based Javascript program to
// check if a number is prime
 
  
// function check whether a number
// is prime or not
function isPrime(n)
{
    // Corner case
    if (n <= 1)
        return false;
  
    // Check from 2 to n-1
    for (let i = 2; i < n; i++)
        if (n % i == 0)
            return false;
  
    return true;
}
  
// Driver Code
 
    isPrime(11) ? document.write(" true" + "<br>") : document.write(" false" + "<br>");
 
// This code is contributed by Mayank Tyagi
 
</script>
Output



 true

Time Complexity: O(\sqrt{n}

Using Recursion :

Recursion can also be used to check if a number between 2 to n – 1 divides n. If we find any number that divides, we return false.

C++




// C++ program to check whether a number
// is prime or not using recursion
#include <iostream>
using namespace std;
 
// function check whether a number
// is prime or not
bool isPrime(int n)
{
    static int i = 2;
 
    // corner cases
    if (n == 0 || n == 1) {
        return false;
    }
 
    // Checking Prime
    if (n == i)
        return true;
 
    // base cases
    if (n % i == 0) {
        return false;
    }
    i++;
    return isPrime(n);
}
 
// Driver Code
int main()
{
 
    isPrime(35) ? cout << " true\n" : cout << " false\n";
    return 0;
}
 
// This code is contributed by yashbeersingh42

Java




// Java program to check whether a number
// is prime or not using recursion
class GFG{
     
static int i = 2;
  
// Function check whether a number
// is prime or not
public static boolean isPrime(int n)
{
     
    // Corner cases
    if (n == 0 || n == 1)
    {
        return false;
    }
   
    // Checking Prime
    if (n == i)
        return true;
         
    // Base cases
    if (n % i == 0)
    {
        return false;
    }
    i++;
    return isPrime(n);
}
 
// Driver Code  
public static void main(String[] args)
{
    if (isPrime(35))
    {
        System.out.println("true");
    }
    else
    {
        System.out.println("false");
    }
}
}
 
// This code is contributed by divyeshrabadiya07

Python3




# Python3 program to check whether a number
# is prime or not using recursion
 
# Function check whether a number
# is prime or not
def isPrime(n, i):
     
    # Corner cases
    if (n == 0 or n == 1):
        return False
     
    # Checking Prime 
    if (n == i):
        return True
     
    # Base cases
    if (n % i == 0):
        return False
     
    i += 1
     
    return isPrime(n, i)
 
# Driver Code
if (isPrime(35, 2)):
  print("true")
else:
  print("false")
   
#  This code is contributed by bunnyram19

C#




// C# program to check whether a number
// is prime or not using recursion
using System;
class GFG {
     
    static int i = 2;
     
    // function check whether a number
    // is prime or not
    static bool isPrime(int n)
    {
      
        // corner cases
        if (n == 0 || n == 1) {
            return false;
        }
      
        // Checking Prime
        if (n == i)
            return true;
      
        // base cases
        if (n % i == 0) {
            return false;
        }
        i++;
        return isPrime(n);
    }
   
  static void Main() {
    if(isPrime(35))
    {
        Console.WriteLine("true");
    }
    else{
        Console.WriteLine("false");
    }
  }
}
 
// This code is contributed by divyesh072019

Javascript




<script>
      // JavaScript program to check whether a number
      // is prime or not using recursion
 
      // function check whether a number
      // is prime or not
      function isPrime(n) {
        var i = 1;
 
        // corner cases
        if (n == 0 || n == 1) {
          return false;
        }
 
        // Checking Prime
        if (n == i) return true;
 
        // base cases
        if (n % i == 0) {
          return false;
        }
        i++;
        return isPrime(n);
      }
 
      // Driver Code
 
      isPrime(35) ? document.write(" true\n") : document.write(" false\n");
       
      // This code is contributed by rdtank.
    </script>
Output
 false

Time Complexity :O(N), Space Complexity :O(N) 

Algorithms to find all prime numbers smaller than the N. 

More problems related to Prime number 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up