Find the prime P using given four integers | 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
function Prime ( n ) { for ( let j = 2 ; j <= Math . sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } function find_prime ( x , xsqmodp , y , ysqmodp ) { let n = x * x - xsqmodp ; let n1 = y * y - ysqmodp ; for ( let j = 2 ; j <= Math . max ( Math . sqrt ( n ) , Math . sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; let j1 = parseInt ( 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 ; } let x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; document . write ( find_prime ( x , xsqmodp , y , ysqmodp ) ) ;