Bertrand's Postulate in C++


Bertrand’s postulates is a mathematical showroom which states that for every number n>3, there exists a prime number p which lies between n and 2n-2.

The formula for Bertrand's Postulate

n < p < 2n -2

Where n is a number such that n>3 and p is a prime number.

Prime number − A number is a prime number if it's only factors are 1 and itself.

A less restrictive formulation of Bertrand’s postulate is

n < p < 2n , for all n>1.

Examples

Number

5

Output

7

Explanation

prime number in range 5 and 2*5 i.e. prime number between 5 and 10

Number

11

Output

13, 17, 19

Explanation

prime number in range 11 and 2*11 i.e. prime number between 11 and 22

Program to find prime number using Bertrand’s postulates

//Program to find prime number using Bertrand’s postulates −

Example

 Live Demo

#include <iostream>
using namespace std;
void printPrime(int n) {
   int flag = 0;
   for (int i = 2; i * i <= n; i++)
      if (n % i == 0) // i is a factor of n
         flag++;
   if(flag == 0)
      cout<<n<<" ";
}
int main() {
   int n = 22;
   cout<<"Prime numbers in range ("<<n<<", "<<2*n<<") :\t";
   for (int p = n + 1; p < 2 * n - 2; p++)
   printPrime(p);
   return 0;
}

Output

Prime numbers in range (22, 44) : 23 29 31 37 41

Updated on: 17-Jul-2020

86 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements