C++ code to find number to disprove given prime hypothesis

C++Server Side ProgrammingProgramming

Suppose we have a number n. Let there is a hypothesis "There exists a positive integer n that for each positive integer m number (n·m + 1) is a prime number". We have to find such m as a counter example to disprove this statement.

So, if the input is like n = 12, then the output will be 10, because 12*10 + 1 = 121 which is not prime.

Steps

To solve this, we will follow these steps −

if n < 3, then:
   return n + 2
Otherwise
   return n - 2

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(int n){
   if (n < 3)
      return n + 2;
   else
      return n - 2;
}
int main(){
   int n = 12;
   cout << solve(n) << endl;
}

Input

12

Output

10
raja
Updated on 15-Mar-2022 06:34:12

Advertisements