The Wayback Machine - https://web.archive.org/web/20240901103958/https://www.geeksforgeeks.org/prime-numbers/
Open In App

Prime Numbers

Last Updated : 17 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Basic
22.2%
2.8L

What are Prime Numbers?

A prime number is defined as a natural number greater than 1 and is divisible by only 1 and itself. 

In other words, the prime number is a positive integer greater than 1 that has exactly two factors, 1 and the number itself. First few prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 . . .

Note: 1 is not either prime or composite. The remaining numbers, except for 1, are classified as prime and composite numbers. 

Prime numbers

Some interesting facts about Prime Numbers:

  • Except for 2, which is the smallest prime number and the only even prime number, all prime numbers are odd numbers.
  • Every prime number can be represented in form of 6n + 1 or 6n – 1 except the prime numbers 2 and 3, where n is any natural number.
  • 2 and 3 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.

Properties of Prime Numbers:

  • Every number greater than 1 can be divided by at least one prime number.
  • Every even positive integer greater than 2 can be expressed as the sum of two primes.
  • Except 2, all other prime numbers are odd. In other words, we can say that 2 is the only even prime number.
  • Two prime numbers are always coprime to each other.
  • Each composite number can be factored into prime factors and individually all of these are unique in nature.

Prime Numbers and Co-prime numbers:

It is important to distinguish between prime numbers and co-prime numbers. Listed below are the differences between prime and co-prime numbers.

  • Coprime numbers are always considered as a pair, whereas a prime number is a single number.
  • Co-prime numbers are numbers that have no common factor except 1. In contrast, prime numbers do not have such a condition.
  • A co-prime number can be either prime or composite, but its greatest common factor (GCF) must always be 1. Unlike composite numbers, prime numbers have only two factors, 1 and the number itself.
  • Example of co-prime: 13 and 15 are co-primes. The factors of 13 are 1 and 13 and the factors of 15 are 1, 3 and 5. We can see that they have only 1 as their common factor, therefore, they are coprime numbers.
  • Example of prime: A few examples of prime numbers are 2, 3, 5, 7 and 11 etc.

How to check whether a number is Prime or not? 

Naive Approach: The naive approach is to

Iterate from 2 to  (n-1) and check if any number in this range divides n. If the number divides n, then it is not a prime number.

Time Complexity: O(N)
Auxiliary Space: O(1)

Naive approach (recursive): 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.

Below is the implementation of the above idea:

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
import java.io.*;

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
Python
# 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
      var i = 2;
      
      function isPrime(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

      isPrime(35) ? document.write(" true\n") : document.write(" false\n");
      
      // This code is contributed by rdtank.
    </script>

Output
 false


Time Complexity: O(N)
Auxiliary Space: O(N) if we consider the recursion stack. Otherwise, it is O(1).

Efficient Approach: An efficient solution is to:

Iterate through all numbers from 2 to ssquare root of n and for every number check if it divides n [because if a number is expressed as n = xy and any of the x or y is greater than the root of n, the other must be less than the root value]. If we find any number that divides, we return false.

Below is the implementation:

C++14
// 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.lang.*;
import java.util.*;

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
Python
# A school method based Python3 program
# to check if a number is prime


# import sqrt from math module
from math import sqrt



# Function check whether a number
# is prime or not
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 __name__ == '__main__':
    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 sqrt(n)
        for (int i = 2; i <= Math.Sqrt(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
Javascript
// 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) ? console.log(" true") : console.log(" false");

// This code is contributed by Mayank Tyagi
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.
?>

Output
true


Time Complexity: O(sqrt(n))
Auxiliary space: O(1)

Another Efficient approach: To check whether  the number is prime or not follow the below idea:

We will deal with a few numbers such as 1, 2, 3, and the numbers which are divisible by 2 and 3 in separate cases and for remaining numbers. Iterate from 5 to sqrt(n) and check for each iteration whether (that value) or (that value + 2) divides n or not and increment the value by 6 [because any prime can be expressed as 6n+1 or 6n-1]. If we find any number that divides, we return false.

Below is the implementation for the above idea:

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)
{
    // Check if n=1 or n=0
    if (n <= 1)
        return false;
    // Check if n=2 or n=3
    if (n == 2 || n == 3)
        return true;
    // Check whether n is divisible by 2 or 3
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    
    // Check from 5 to square root of n
    // Iterate i by (i+6)
    for (int i = 5; i <= sqrt(n); i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

// Driver Code
int main()
{
    isPrime(11) ? cout << "true\n" : cout << "false\n";
    return 0;
}
//  This code is contributed by Suruchi kumari
C
// A school method based C program to
// check if a number is prime
#include <math.h>
#include <stdio.h>

// Function check whether a number
// is prime or not
int isPrime(int n)
{
    // Check if n=1 or n=0
    if (n <= 1)
        return 0;
    // Check if n=2 or n=3
    if (n == 2 || n == 3)
        return 1;
    // Check whether n is divisible by 2 or 3
    if (n % 2 == 0 || n % 3 == 0)
        return 0;
    // Check from 5 to square root of n
    // Iterate i by (i+6)
    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return 0;

    return 1;
}

// Driver Code
int main()
{
    if (isPrime(11) == 1)
        printf("true\n");
    else
        printf("false\n");
    return 0;
}
// This code is contributed by Suruchi Kumari
Java
// Java program to check whether a number
import java.lang.*;
import java.util.*;

class GFG {

    // Function check whether a number
    // is prime or not
    public static boolean isPrime(int n)
    {
        if (n <= 1)
            return false;

        // Check if n=2 or n=3
        if (n == 2 || n == 3)
            return true;

        // Check whether n is divisible by 2 or 3
        if (n % 2 == 0 || n % 3 == 0)
            return false;

        // Check from 5 to square root of n
        // Iterate i by (i+6)
        for (int i = 5; i <= Math.sqrt(n); i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;

        return true;
    }

    // Driver Code
    public static void main(String[] args)
    {
        if (isPrime(11)) {
            System.out.println("true");
        }
        else {
            System.out.println("false");
        }
    }
}

// This code is contributed by Sayan Chatterjee
Python
import math

def is_prime(n: int) -> bool:
    
    # Check if n=1 or n=0
    if n <= 1:
        return "false"
    
    # Check if n=2 or n=3
    if n == 2 or n == 3:
        return "true"
    
    # Check whether n is divisible by 2 or 3
    if n % 2 == 0 or n % 3 == 0:
        return "false"
    
    # Check from 5 to square root of n
    # Iterate i by (i+6)
    for i in range(5, int(math.sqrt(n))+1, 6):
        if n % i == 0 or n % (i + 2) == 0:
            return "false"

    return "true"

if __name__ == '__main__':
    print(is_prime(11))
C#
// C# program to check whether a number
using System;
class GFG {

    // Function check whether a number
    // is prime or not
    public static bool isPrime(int n)
    {
        if (n <= 1)
            return false;

        // Check if n=2 or n=3
        if (n == 2 || n == 3)
            return true;

        // Check whether n is divisible by 2 or 3
        if (n % 2 == 0 || n % 3 == 0)
            return false;

        // Check from 5 to square root of n
        // Iterate i by (i+6)
        for (int i = 5; i <= Math.Sqrt(n); i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;

        return true;
    }

    // Driver Code
    public static void Main(String[] args)
    {
        if (isPrime(11)) {
            Console.WriteLine("true");
        }
        else {
            Console.WriteLine("false");
        }
    }
}

// This code is contributed by Abhijeet
// Kumar(abhijeet_19403)
Javascript
// A school method based JS program to
// check if a number is prime


// Function check whether a number
// is prime or not
function isPrime(n)
{
    // Check if n=1 or n=0
    if (n <= 1)
        return false;
    // Check if n=2 or n=3
    if (n == 2 || n == 3)
        return true;
    // Check whether n is divisible by 2 or 3
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    // Check from 5 to square root of n
    // Iterate i by (i+6)
    for (var i = 5; i <= Math.sqrt(n); i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;

    return true;
}

// Driver Code
isPrime(11) ? console.log("true") : console.log("false");


//  This code is contributed by phasing17

Output
true


Time complexity: O(sqrt(n))
Auxiliary space: O(1)

Efficient solutions

Algorithms to find all prime numbers smaller than the N. 

More problems related to Prime number 



Similar Reads

Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between sum of non-prime numbers and prime numbers. Note: 1 is neither prime nor non-prime. Examples: Input : 1 3 5 10 15 7 Output : 10 Explanation: Sum of non-primes = 25 Sum of primes = 15 Input : 3 4 6 7 Output : 0 Naive Approach: A simple solution is to travers
15+ min read
Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between product of non-prime numbers and prime numbers.Note: 1 is neither prime nor non-prime.Examples: Input : arr[] = {1, 3, 5, 10, 15, 7} Output : 45 Explanation : Product of non-primes = 150 Product of primes = 105 Input : arr[] = {3, 4, 6, 7} Output : 3 Naive
15 min read
Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
Given an array arr[] of N positive integers, the task is to calculate the absolute difference between XOR of non-prime numbers and prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 4 Xor of non-primes = 10 ^ 15 = 5 Xor of primes = 3 ^ 5 ^ 7 = 1 |5 - 1| = 4Input: arr[] = {3, 4, 6, 7} Outp
8 min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N &gt; 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers â‰Ī N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number â‰Ī N, which is even and a product of two distinct prime numbers (2 an
7 min read
Count prime numbers up to N that can be represented as a sum of two prime numbers
Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers. Examples: Input: N = 6Output: 1Explanation:5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e., 5 = 2 + 3, where 2, 3 and 5 are all primes.There
15+ min read
Count prime numbers that can be expressed as sum of consecutive prime numbers
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741 = 2 + 3 + 5 + 7 + 11 + 13Therefore, the count is 3
8 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Prime numbers after prime P with sum S
Given three numbers sum S, prime P, and N, find all N prime numbers after prime P such that their sum is equal to S.Examples : Input : N = 2, P = 7, S = 28 Output : 11 17 Explanation : 11 and 17 are primes after prime 7 and (11 + 17 = 28) Input : N = 3, P = 2, S = 23 Output : 3 7 13 5 7 11 Explanation : 3, 5, 7, 11 and 13 are primes after prime 2.
13 min read
Print the nearest prime number formed by adding prime numbers to N
Given a number N. The task is to print the nearest prime if the number is not prime by making it prime by adding prime numbers sequentially from 2. Examples: Input: N = 8 Output: 13 8 is not prime, so add the first prime to it to get 10 10 is not prime, hence add the second prime, i.e., 3 to get 13 which is prime. Input: N = 45 Output: 47 Naive App
11 min read
Check if a prime number can be expressed as sum of two Prime Numbers
Given a prime number N. The task is to check if it is possible to express N as the sum of two separate prime numbers.Note: The range of N is less than 108. Examples: Input: N = 13Output: YesExplanation: The number 13 can be written as 11 + 2, here 11 and 2 are both prime.Input: N = 11Output: NoRecommended: Please try your approach on {IDE} first, b
13 min read
Print prime numbers with prime sum of digits in an array
Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[] = {2, 3, 19, 13, 25, 7} Output: 2, 3, 7 A simple
8 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Sum of prime numbers without odd prime digits
Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosthenes to store all prime numbers.Next check for eac
8 min read
Permutation of first N positive integers such that prime numbers are at prime indices
Given an integer N, the task is to find the number of permutations of first N positive integers such that prime numbers are at prime indices (for 1-based indexing).Note: Since, the number of ways may be very large, return the answer modulo 109 + 7. Examples: Input: N = 3 Output: 2 Explanation: Possible permutation of first 3 positive integers, such
6 min read
Permutation of first N positive integers such that prime numbers are at prime indices | Set 2
Given an integer N, the task is to find the number of permutations of first N positive integers such that prime numbers are at prime indices (for 1-based indexing).Note: Since, the number of ways may be very large, return the answer modulo 109 + 7.Examples: Input: N = 3 Output: 2 Explanation: Possible permutation of first 3 positive integers, such
6 min read
Count numbers in a given range whose count of prime factors is a Prime Number
Given a 2D array Q[][] of size N * 2 representing queries of the form {L, R}. For each query, the task is to print the count of numbers in the range [L, R] with a count of prime factors equal to a prime number. Examples: Input: Q[][] = {{4, 8}, {30, 32}} Output: 3 2 Explanation: Query 1: Prime factors of 4 = {2, 2} and count of prime factors = 2 Pr
15 min read
Count of unordered pairs of semi-prime numbers with prime sum in range [1, N]
Given a positive integer N, the task is to find the number of unordered pairs of semi-prime numbers in the range [1, N] such that their sum is prime. Examples: Input: N = 25Output: 5Explanation:The valid pairs of semi-prime numbers whose sum is also prime are (10, 21), (14, 15), (15, 22), (20, 21), and (21, 22). The count of such numbers is 5. Inpu
8 min read
Count prime numbers in range [L, R] whose single digit sum is also prime
Given two integers L and R. The task is to count the prime numbers in the range [L, R], whose single sum is also a prime number. A single sum is obtained by adding the digits of a number until a single digit is left. Examples Input: L = 5, R = 20 Output: 3Explanation: Prime numbers in the range L = 5 to R = 20 are {5, 7, 11, 13, 17, 19}Their "singl
13 min read
Check prime numbers in Array against prime index positions
Given an array arr[], the task is to check for each prime number in the array, from its position(indexing from 1) to each non-prime position index, and check if the number present in that position is prime or not. Examples: Input: arr[] = {5, 9, 3, 7, 8, 2}Output: PrimeExplanation: First prime number found is 5. For 5, two non-prime positions are 4
11 min read
Numbers less than N which are product of exactly two distinct prime numbers
Given a number [Tex]N [/Tex]. The task is to find all such numbers less than N and are a product of exactly two distinct prime numbers. For Example, 33 is the product of two distinct primes i.e 11 * 3, whereas numbers like 60 which has three distinct prime factors i.e 2 * 2 * 3 * 5.Note: These numbers cannot be a perfect square.Examples: Input : N
8 min read
Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
Given an array of positive numbers, the task is to find the bitwise AND of the sum of non-prime numbers and the sum of prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 9 Sum of non-primes = 10 + 15 = 25 Sum of primes = 3 + 5 + 7 = 15 25 &amp; 15 = 9Input: arr[] = {3, 4, 6, 7} Output: 10
9 min read
Count of numbers upto M divisible by given Prime Numbers
Given an array arr[] of Prime Numbers and a number M, the task is to count the number of elements in the range [1, M] that are divisible by any of the given prime numbers. Examples: Input: arr[] = {2, 3, 5, 7} M = 100 Output: 78 Explanation:In total there are 78 numbers that are divisible by either of 2 3 5 or 7. Input: arr[] = {2, 5, 7, 11} M = 20
5 min read
Count numbers up to N that can be expressed as powers of Prime Numbers
Given an integer N, the task is to count numbers from the range [1, N] which are the power of prime numbers. Examples: Input: N = 6Output: 3Explanation:Numbers from the range [1, 6] that can be expressed as powers of prime numbers are:2 = 213 = 314 = 225 = 51 Input: N = 9Output: 7Explanation:Numbers from the range [1, 9] that can be expressed as po
9 min read
Bitwise XOR of first N natural numbers that are product of two distinct Prime Numbers
Given a positive integer N, the task is to calculate the Bitwise XOR of first N numbers which are a product of exactly two distinct prime numbers. Examples: Input: N = 20Output: 7Explanation: The numbers from the range [1, 20] which are a product of exactly two distinct prime numbers are {6, 10, 12, 14, 15, 18, 20}.Bitwise XOR of these numbers = 6
8 min read
Sum of each element raised to (prime-1) % prime
Given an array arr[] and a positive integer P where P is prime and none of the elements of array are divisible by P. Find sum of all the elements of the array raised to the power P - 1 i.e. arr[0]P - 1 + arr[1]P - 1 + ... + arr[n - 1]P - 1 and print the result modulo P.Examples: Input: arr[] = {2, 5}, P = 3 Output: 2 22 + 52 = 29 and 29 % 3 = 2Inpu
3 min read
Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
14 min read
Check whether the sum of prime elements of the array is prime or not
Given an array having N elements. The task is to check if the sum of prime elements of the array is prime or not. Examples: Input: arr[] = {1, 2, 3} Output: Yes As there are two primes in the array i.e. 2 and 3. So, the sum of prime is 2 + 3 = 5 and 5 is also prime. Input: arr[] = {2, 3, 2, 2} Output: No Approach: First find prime number up to 10^5
10 min read
Count prime pairs whose difference is also a Prime Number
Given an integer N, the task is to count the number of pairs of prime numbers in the range [1, N] such that the difference between elements of each pair is also a prime number. Examples: Input: N = 5 Output: 2 Explanations: Pair of prime numbers in the range [1, 5] whose difference between elements is also a prime number are: (2, 5) = 3 (Prime numb
9 min read