Given a positive integer, check if the number is prime or not. A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Examples of the first few prime numbers are {2, 3, 5, …}
Examples :
Input: n = 11
Output: true
Input: n = 15
Output: false
Input: n = 1
Output: false
School Method: A simple 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.
Below is the implementation of this method.
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
int main()
{
isPrime(11) ? cout << " true\n" : cout << " false\n" ;
isPrime(15) ? cout << " true\n" : cout << " false\n" ;
return 0;
}
|
Java
class GFG {
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i < n; i++)
if (n % i == 0 )
return false ;
return true ;
}
public static void main(String args[])
{
if (isPrime( 11 ))
System.out.println( " true" );
else
System.out.println( " false" );
if (isPrime( 15 ))
System.out.println( " true" );
else
System.out.println( " false" );
}
}
|
Python3
def isPrime(n):
if n < = 1 :
return False
for i in range ( 2 , n):
if n % i = = 0 :
return False
return True
print ( "true" ) if isPrime( 11 ) else print ( "false" )
print ( "true" ) if isPrime( 14 ) else print ( "false" )
|
C#
using System;
namespace prime {
public class GFG {
public static bool isprime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
public static void Main()
{
if (isprime(11))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
if (isprime(15))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
}
|
PHP
<?php
function isPrime( $n )
{
if ( $n <= 1) return false;
for ( $i = 2; $i < $n ; $i ++)
if ( $n % $i == 0)
return false;
return true;
}
$tet = isPrime(11) ? " true\n" :
" false\n" ;
echo $tet ;
$tet = isPrime(15) ? " true\n" :
" false\n" ;
echo $tet ;
?>
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1) return false ;
for (let i = 2; i < n; i++)
if (n % i == 0)
return false ;
return true ;
}
isPrime(11)? document.write( " true" + "<br>" ): document.write( " false" + "<br>" );
isPrime(15)? document.write( " true" + "<br>" ): document.write( " false" + "<br>" );
</script>
|
Time complexity: O(n)
Auxiliary Space: O(1)
Optimized School Method: We can do the following optimizations: Instead of checking till n, we can check till √n because a larger factor of n must be a multiple of a smaller factor that has been already checked. The implementation of this method is as follows:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i <= sqrt (n); i++)
if (n % i == 0)
return false ;
return true ;
}
int main()
{
isPrime(11) ? cout << " true\n" : cout << " false\n" ;
isPrime(15) ? cout << " true\n" : cout << " false\n" ;
return 0;
}
|
Java
class GFG {
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
if (n % i == 0 )
return false ;
return true ;
}
public static void main(String args[])
{
if (isPrime( 11 ))
System.out.println( " true" );
else
System.out.println( " false" );
if (isPrime( 15 ))
System.out.println( " true" );
else
System.out.println( " false" );
}
}
|
Python3
import math
def isPrime(n):
if (n < = 1 ):
return False
for i in range ( 2 , int (math.sqrt(n)) + 1 ):
if (n % i = = 0 ):
return False
return True
print ( "true" ) if isPrime( 11 ) else print ( "false" )
print ( "true" ) if isPrime( 15 ) else print ( "false" )
|
C#
using System;
namespace prime {
public class GFG {
public static bool isprime( int n)
{
if (n <= 1)
return false ;
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
public static void Main()
{
if (isprime(11))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
if (isprime(15))
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1) return false ;
for (let i = 2; i*i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
if (isPrime(11))
document.write( " true" + "<br/>" );
else
document.write( " false" + "<br/>" );
if (isPrime(15))
document.write( " true" + "<br/>" );
else
document.write( " false" + "<br/>" );
</script>
|
Time Complexity: O(√n)
Auxiliary Space: O(1)
Another approach: It is based on the fact that all primes greater than 3 are of the form 6k ± 1, where k is any integer greater than 0. This is because all integers can be expressed as (6k + i), where i = −1, 0, 1, 2, 3, or 4. And note that 2 divides (6k + 0), (6k + 2), and (6k + 4) and 3 divides (6k + 3). So, a more efficient method is to test whether n is divisible by 2 or 3, then to check through all numbers of the form 6k ± 1 <= √n. This is 3 times faster than testing all numbers up to √n. (Source: wikipedia).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n == 2 || n == 3)
return true ;
if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
int main()
{
isPrime(11) ? cout << " true\n" : cout << " false\n" ;
isPrime(15) ? cout << " true\n" : cout << " false\n" ;
return 0;
}
|
Java
class GFG {
static boolean isPrime( int n)
{
if (n == 2 || n == 3 )
return true ;
if (n <= 1 || n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i += 6 )
{
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
}
return true ;
}
public static void main(String args[])
{
if (isPrime( 11 ))
System.out.println( " true" );
else
System.out.println( " false" );
if (isPrime( 15 ))
System.out.println( " true" );
else
System.out.println( " false" );
}
}
|
Python3
import math
def isPrime(n):
if n = = 2 or n = = 3 :
return True
elif n < = 1 or n % 2 = = 0 or n % 3 = = 0 :
return False
for i in range ( 5 , int (math.sqrt(n)) + 1 , 6 ):
if n % i = = 0 or n % (i + 2 ) = = 0 :
return False
return True
print (isPrime( 11 ))
print (isPrime( 20 ))
|
C#
using System;
class GFG {
static bool isPrime( int n)
{
if (n == 2 || n == 3)
return true ;
if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i += 6)
{
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
public static void Main(String[] args)
{
if (isPrime(11))
Console.WriteLine( " true" );
else
Console.WriteLine( " false" );
if (isPrime(15))
Console.WriteLine( " true" );
else
Console.WriteLine( " false" );
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n == 2 || n == 3)
return true ;
if (n <= 1 || n % 2 == 0 || n % 3 == 0)
return false ;
for (let i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false ;
}
return true ;
}
isPrime(11) ? document.write( " true" + "<br/>" ) : document.write( " false" + "<br/>" );
isPrime(15) ? document.write( " true" + "<br/>" ) : document.write( " false" + "<br/>" );
</script>
|
Time Complexity: O(√n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!