给定四个整数X,Y,X 2 %P,Y 2 %P,其中P是质数。任务是找到素数P。
注意:答案总是存在的。
例子:
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 3
Input : X = 4, XsqmodP = 1, Y = 5, YsqmodP = 0
Output : 5
方法 :
从以上数字我们可以得出,
X2 – XsqmodP = 0 mod P
Y2 – YsqmodP = 0 mod P
现在,从这两个方程中找到所有公有质因数,并检查它是否满足原始方程,如果满足(其中一个将始终存在答案),那就是答案。
下面是上述方法的实现:
C++
// CPP program to possible prime number
#include
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
Python3
# Python3 program to possible prime number
from math import sqrt
# Function to check if a
# number is prime or not
def Prime(n):
for j in range(2, int(sqrt(n)) + 1, 1):
if (n % j == 0):
return False
return True
# Function to find possible prime number
def find_prime(x, xsqmodp, y, ysqmodp):
n = x * x - xsqmodp
n1 = y * y - ysqmodp
# Find a possible prime number
for j in range(2, max(int(sqrt(n)),
int(sqrt(n1))), 1):
if (n % j == 0 and (x * x) % j == xsqmodp and
n1 % j == 0 and (y * y) % j == ysqmodp):
if (Prime(j)):
return j
j1 = n // j
if (n % j1 == 0 and (x * x) % j1 == xsqmodp and
n1 % j1 == 0 and (y * y) % j1 == ysqmodp):
if (Prime(j1)):
return j1
j1 = n1 // j
if (n % j1 == 0 and (x * x) % j1 == xsqmodp and
n1 % j1 == 0 and (y * y) % j1 == ysqmodp):
if (Prime(j1)):
return j1
# Last condition
if (n == n1):
return n
# Driver code
if __name__ == '__main__':
x = 3
xsqmodp = 0
y = 5
ysqmodp = 1
# Function call
print(find_prime(x, xsqmodp, y, ysqmodp))
# This code is contributed by
# Surendra_Gangwar
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..
输出:
3