Find the prime P using given four integers
Given four integers X, Y, X2%P, Y2%P, where P is a prime number. The task is to find prime P.
Note: Answer always exsits.
Examples:
Input : X = 3, XsqmodP = 0, Y = 5, YsqmodP = 1
Output : 3
When x = 3, x2 = 9, and 9 modulo P is 0. So possible value of p is 3
When x = 5, x2 = 25, and 25 modulo P is 1. So possible value of p is 3Input : X = 4, XsqmodP = 1, Y = 5, YsqmodP = 0
Output : 5
Approach :
From above numbers we get,
X2 – XsqmodP = 0 mod P
Y2 – YsqmodP = 0 mod P
Now find all the common prime factors from both the equation, and check if it satisfies the original equation, If it does (one of them will since answer always exists) then that’s the answer.
Below is the implementation of the above approach :
C++
// CPP program to possible prime number #include <bits/stdc++.h> using namespace std; // Function to check if a // number is prime or not bool Prime(int n) { for (int j = 2; j <= sqrt(n); j++) if (n % j == 0) return false; return true; } // Function to find possible prime number int find_prime(int x, int xsqmodp , int y, int ysqmodp) { int n = x*x - xsqmodp; int n1 = y*y - ysqmodp; // Find a possible prime number 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; } // Last condition if (n == n1) return n; } // Driver code int main() { int x = 3, xsqmodp = 0, y = 5, ysqmodp = 1; // Function call cout << find_prime(x, xsqmodp, y, ysqmodp); return 0; } |
Java
// Java program to possible prime number import java.util.*; class GFG { // Function to check if a // number is prime or not static boolean Prime(int n) { for (int j = 2; j <= Math.sqrt(n); j++) if (n % j == 0) return false; return true; } // Function to find possible prime number static int find_prime(int x, int xsqmodp , int y, int ysqmodp) { int n = x * x - xsqmodp; int n1 = y * y - ysqmodp; // Find a possible prime number for (int 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; 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; } // Last condition if (n == n1) return n; return Integer.MIN_VALUE; } // Driver code public static void main(String[] args) { int x = 3, xsqmodp = 0, y = 5, ysqmodp = 1; // Function call System.out.println(find_prime(x, xsqmodp, y, ysqmodp)); } } // This code is contributed by PrinciRaj1992 |
C#
// C# program to possible prime number
using System;
class GFG
{
// Function to check if a
// number is prime or not
static bool Prime(int n)
{
for (int j = 2;
j <= Math.Sqrt(n); j++)
if (n % j == 0)
return false;
return true;
}
// Function to find possible prime number
static int find_prime(int x, int xsqmodp ,
int y, int ysqmodp)
{
int n = x * x - xsqmodp;
int n1 = y * y - ysqmodp;
// Find a possible prime number
for (int 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;
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;
}
// Last condition
if (n == n1)
return n;
return int.MinValue;
}
// Driver code
public static void Main()
{
int x = 3, xsqmodp = 0,
y = 5, ysqmodp = 1;
// Function call
Console.WriteLine(find_prime(x, xsqmodp,
y, ysqmodp));
}
}
// This code is contributed by anuj_67..
[tabbyending]
3
Recommended Posts:
- Quick ways to check for Prime and find next Prime in Java
- Find coordinates of a prime number in a Prime Spiral
- Find two integers A and B such that A ^ N = A + N and B ^ N = B + N
- Find N integers with given difference between product and sum
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Find three integers less than or equal to N such that their LCM is maximum
- Find n positive integers that satisfy the given equations
- Find a pair with maximum product in array of Integers
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Find integers that divides maximum number of elements of the array
- Median in a stream of integers (running integers)
- Find next palindrome prime
- Given a number x , find y such that x*y + 1 is not a prime
- Find two prime numbers with given sum
- Find if nCr is divisible by the given prime
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



