Prime Numbers
A prime number is a whole 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 represented in form of 6n+1 or 6n-1 except 2 and 3, where n is natural number.
- Two and Three are only two consecutive natural numbers which are prime too.
- 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 - Fermat’s Little Theorem: If n is a prime number, then for every a, 1 <= a < n,
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 n-1 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>usingnamespacestd;// function check whether a number// is prime or notboolisPrime(intn){// Corner caseif(n <= 1)returnfalse;// Check from 2 to n-1for(inti = 2; i < n; i++)if(n % i == 0)returnfalse;returntrue;}// Driver Programintmain(){isPrime(11) ? cout <<" true\n":cout <<" false\n";return0;}chevron_rightfilter_noneJava
// A school method based Java program to// check if a number is primeimportjava.util.*;classGFG {// function check whether a number// is prime or notstaticbooleanisPrime(intn){// Corner caseif(n <=1)returnfalse;// Check from 2 to n-1for(inti =2; i < n; i++)if(n % i ==0)returnfalse;returntrue;}/* Driver program */publicstaticvoidmain(String[] args){if(isPrime(11))System.out.println(" true") ;elseSystem.out.println(" false");}}// This code is contributed by Arnav Kr. Mandalchevron_rightfilter_nonePython3
# A school method based Python3 program# to check if a number is prime# function check whether a number# is prime or notdefisPrime(n):# Corner caseif(n <=1):returnFalse# Check from 2 to n-1foriinrange(2, n):if(n%i==0):returnFalsereturnTrue# Driver ProgramifisPrime(11):print("true")else:print("false")# This code is contributed by Sachin Bishtchevron_rightfilter_noneC#
// A school method based C# program to// check if a number is primeusingSystem;classGFG{// function check whether a// number is prime or notstaticboolisPrime(intn){// Corner caseif(n <= 1)returnfalse;// Check from 2 to n-1for(inti = 2; i < n; i++)if(n % i == 0)returnfalse;returntrue;}// Driver CodestaticvoidMain(){if(isPrime(11))Console.Write(" true") ;elseConsole.Write(" false");}}// This code is contributed by Sam007chevron_rightfilter_nonePHP
<?php// A school method based PHP program to// check if a number is prime// function check whether a number// is prime or notfunctionisPrime($n){// Corner caseif($n<= 1)returnfalse;// Check from 2 to n-1for($i= 2;$i<$n;$i++)if($n%$i== 0)returnfalse;returntrue;}// Driver Codeif(isPrime(11))echo("true");elseecho("false");// This code is contributed by Ajit.?>chevron_rightfilter_none
Output :
Time complexity :O(n)True
- Efficient solutions
Algorithms to find all prime number smaller the N.
- Sieve of Eratosthenes
- Sieve of Eratosthenes in 0(n) time complexity
- Segmented Sieve
- Sieve of Sundaram
- Bitwise Sieve
- Recent Articles on Sieve!
More problems related to Prime number
- Find two distinct prime numbers with given product
- Print all prime numbers less than or equal to N
- Recursive program for prime number
- Find two prime numbers with given sum
- Find the highest occurring digit in prime numbers in a range
- Prime Factorization using Sieve O(log n) for multiple queries
- Program to print all prime factors of a given number
- Least prime factor of numbers till n
- Prime factors of LCM of array elements – GeeksforGeeks
- Program for Goldbach’s Conjecture
- Prime numbers and Fibonacci
- Composite Number
- Recent Articles on Prime Numbers!
Recommended Posts:
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Check if a prime number can be expressed as sum of two Prime Numbers
- Print prime numbers with prime sum of digits in an array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of prime numbers without odd prime digits
- Bitwise AND of the sum of prime numbers and the sum of composite numbers in an array
- Numbers less than N which are product of exactly two distinct prime numbers
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Prime numbers after prime P with sum S
- Almost Prime Numbers
- Sum of the first N Prime numbers
- Prime numbers and Fibonacci
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

