Bertrand’s Postulate
Last Updated :
29 Mar, 2024
In mathematics, Bertrand’s Postulate states that there is a prime number in the range to where n is a natural number and n >= 4. It has been proved by Chebyshev and later by Ramanujan. A lenient form of the postulate states that there exists a prime in range n to 2n for any n(n >= 2).
There exists a prime p for for all n <= 4. The less stricter form states that there exists a prime p. For for all n <= 2.
Examples:
For n = 4 and 2*n – 2 = 6,
5 is a prime number in the range (4, 6).
For n = 5 and 2*n – 2 = 8,
7 is a prime number in the range (5, 8).
For n = 6 and 2*n – 2 = 10,
7 is a prime number in the range (6, 10).
For n = 7 and 2*n – 2 = 12,
11 is a prime number in the range (7, 12).
For n = 8 and 2*n – 2 = 14,
11 is a prime number in the range (8, 14).
Examples :
Input: n = 4
Output: Prime numbers in range (4, 6) are 5
Input: n = 5
Output: Prime numbers in range (5, 8) are 7
Input: n = 6
Output: Prime numbers in range (6, 10) are 7
C++
#include <bits/stdc++.h>
using namespace std;
bool isprime( int n)
{
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
int main()
{
int n = 10;
cout << "Prime numbers in range (" << n << ", "
<< 2 * n - 2 << ")\n" ;
for ( int i = n + 1; i < 2 * n - 2; i++)
if (isprime(i))
cout << i << "\n" ;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isprime( int n)
{
for ( int i = 2 ; i * i <= n; i++)
if (n % i == 0 )
return false ;
return true ;
}
public static void main (String[] args)
{
int n = 10 ;
System.out.println( "Prime numbers in range (" +
n + ", " + ( 2 * n - 2 ) + ")" );
for ( int i = n + 1 ; i < 2 * n - 2 ; i++)
if (isprime(i))
System.out.println(i);
}
}
|
Python3
def isprime(n):
i = 2 ;
while (i * i < = n):
if (n % i = = 0 ):
return False ;
i = i + 1 ;
return True ;
n = 10 ;
print ( "Prime numbers in range (" , n ,
", " , 2 * n - 2 , ")" );
i = n + 1 ;
while (i < ( 2 * n - 2 )):
if (isprime(i)):
print (i);
i = i + 1 ;
|
C#
using System;
class GFG
{
static bool isprime( int n)
{
for ( int i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
public static void Main ()
{
int n = 10;
Console.WriteLine( "Prime numbers in range (" +
n + ", " + (2 * n - 2) + ")" );
for ( int i = n + 1; i < 2 * n - 2; i++)
if (isprime(i))
Console.WriteLine(i);
}
}
|
PHP
<?php
function isprime( $n )
{
for ( $i = 2; $i * $i <= $n ; $i ++)
if ( $n % $i == 0)
return false;
return true;
}
$n = 10;
echo "Prime numbers in range (" , $n ,
", " , 2 * $n - 2 , ")\n" ;
for ( $i = $n + 1; $i < 2 * $n - 2; $i ++)
if (isprime( $i ))
echo $i , "\n" ;
?>
|
Javascript
<script>
function isprime(n)
{
for (let i = 2; i * i <= n; i++)
if (n % i == 0)
return false ;
return true ;
}
let n = 10;
document.write(
"Prime numbers in range (" + n + ", " + (2 * n - 2) + ")" +
"</br>"
);
for (let i = n + 1; i < 2 * n - 2; i++)
if (isprime(i))
document.write(i + "</br>" );
</script>
|
Output : Prime numbers in range (10, 18)
11
13
17
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...