给定一个整数N ,任务是找到两个非负整数A和B ,使得A 2 -B 2 = N。如果不存在这样的整数,则打印-1 。
例子:
Input: N = 7
Output: 4 3
Explanation:
The two integers 4 and 3 can be represented as 42 – 32 = 7.
Input: N = 6
Output: -1
Explanation:
No pair of (A, B) exists that satisfies the required condition.
方法:
- A 2 – B 2可以表示为(A – B)*(A + B) 。
A2 – B2 = (A – B) * (A + B)
- 因此,对于A 2 -B 2等于N , (A + B)和(A-B)都应为N的因数。
- 考虑到A + B和A – B分别等于C和D ,则C和D必须是N的因数,这样C <= D且C和D应该具有相同的奇偶性。
- 因此,为了解决这个问题,我们只需要找到满足上述条件的任何一对C和D即可。如果不存在这样的C&D ,则打印-1 。
下面是上述方法的实现:
C++
// C++ Program to find two numbers
// with difference of their
// squares equal to N
#include
using namespace std;
// Function to check and print
// the required two positive integers
void solve(int n)
{
// Iterate till sqrt(n) to find
// factors of N
for (int x = 1; x <= sqrt(n); x++) {
// Check if x is one
// of the factors of N
if (n % x == 0) {
// Store the factor
int small = x;
// Compute the other factor
int big = n / x;
// Check if the two factors
// are of the same parity
if (small % 2 == big % 2) {
// Compute a and b
int a = (small + big) / 2;
int b = (big - small) / 2;
cout << a << " "
<< b << endl;
return;
}
}
}
// If no pair exists
cout << -1 << endl;
}
// Driver Code
int main()
{
int n = 7;
solve(n);
return 0;
}
Java
// Java Program to find two numbers
// with difference of their
// squares equal to N
import java.util.*;
class GFG{
// Function to check and print
// the required two positive integers
static void solve(int n)
{
// Iterate till sqrt(n) to find
// factors of N
for (int x = 1; x <= Math.sqrt(n); x++)
{
// Check if x is one
// of the factors of N
if (n % x == 0)
{
// Store the factor
int small = x;
// Compute the other factor
int big = n / x;
// Check if the two factors
// are of the same parity
if (small % 2 == big % 2)
{
// Compute a and b
int a = (small + big) / 2;
int b = (big - small) / 2;
System.out.print(a + " " + b);
return;
}
}
}
// If no pair exists
System.out.print(-1);
}
// Driver Code
public static void main(String args[])
{
int n = 7;
solve(n);
}
}
// This code is contributed by Code_Mech
Python3
# Python3 Program to find two numbers
# with difference of their
# squares equal to N
from math import sqrt
# Function to check and print
# the required two positive integers
def solve(n) :
# Iterate till sqrt(n) to find
# factors of N
for x in range(1, int(sqrt(n)) + 1) :
# Check if x is one
# of the factors of N
if (n % x == 0) :
# Store the factor
small = x;
# Compute the other factor
big = n // x;
# Check if the two factors
# are of the same parity
if (small % 2 == big % 2) :
# Compute a and b
a = (small + big) // 2;
b = (big - small) // 2;
print(a, b) ;
return;
# If no pair exists
print(-1);
# Driver Code
if __name__ == "__main__" :
n = 7;
solve(n);
# This code is contributed by AnkitRai01
C#
// C# Program to find two numbers
// with difference of their
// squares equal to N
using System;
class GFG{
// Function to check and print
// the required two positive integers
static void solve(int n)
{
// Iterate till sqrt(n) to find
// factors of N
for (int x = 1; x <= Math.Sqrt(n); x++)
{
// Check if x is one
// of the factors of N
if (n % x == 0)
{
// Store the factor
int small = x;
// Compute the other factor
int big = n / x;
// Check if the two factors
// are of the same parity
if (small % 2 == big % 2)
{
// Compute a and b
int a = (small + big) / 2;
int b = (big - small) / 2;
Console.WriteLine(a + " " + b);
return;
}
}
}
// If no pair exists
Console.WriteLine(-1);
}
// Driver Code
public static void Main()
{
int n = 7;
solve(n);
}
}
// This code is contributed by Code_Mech
输出:
4 3
时间复杂度: O(sqrt(N))
辅助空间: O(1)