Find the prime P using given four integers | CPP program to possible prime number ; Function to check if a number is prime or not ; Function to find possible prime number ; Find a possible prime number ; Last condition ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Prime ( int n ) { for ( int j = 2 ; j <= sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= max ( sqrt ( n ) , sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; } int main ( ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; cout << find_prime ( x , xsqmodp , y , ysqmodp ) ; return 0 ; }