给定正数N。任务是找出最小的理想平方数A,使N + A也是理想平方数或返回-1。
例子:
Input: N = 3
Output: 1
Explanation:
As 1 + 3 = 4 = 22
Input: N=1
Output: -1
天真的方法:
从{1,2,3,4,5…}遍历M,并检查(N + M * M)是否为理想的平方数。
高效方法:
- 关于观察,我们有一个等式:
- N + (X * X) = (M * M) where N is given and M and X are unknown.
- We can rearrange it and get:
- N = (M * M) – (X * X)
- N = (M + X) * (M – X)
- 现在我们可以看到,要获得N,我们需要找到N的因数.N的因数可以在O(N)时间中获得。但是通过这种方法可以将其优化为O(N ^ 1/2)。
- 令N的因子为a,b =(N / a)。因此,从上面的等式a =(M – X)和b =(M + X),求解后,我们可以获得X =(b – a)/ 2的值。
下面是上述方法的实现:
C++
// C++ code to find out the smallest
// perfect square X which when added to N
// yields another perfect square number.
#include
using namespace std;
long SmallestPerfectSquare(long N){
// X is the smallest perfect
// square number
long X = (long)1e9;
long ans;
// Loop from 1 to square root of N
for(int i = 1; i < sqrt(N); i++)
{
// Condition to check whether i
// is factor of N or not
if (N % i == 0)
{
long a = i;
long b = N / i;
// Condition to check whether
// factors satisfies the
// equation or not
if((b - a != 0) && ((b - a) % 2 == 0))
{
// Stores minimum value
X = min(X, (b - a) / 2);
}
}
}
// Return if X * X if X is not equal
// to 1e9 else return -1
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
// Driver code
int main()
{
long N = 3;
cout << SmallestPerfectSquare(N);
return 0;
}
// This code is contributed by AnkitRai01
Java
// Java code to find out the smallest
// perfect square X which when added to N
// yields another perfect square number.
public class GFG {
static long SmallestPerfectSquare(long N)
{
// X is the smallest perfect
// square number
long X = (long)1e9;
long ans;
// Loop from 1 to square root of N
for(int i = 1; i < Math.sqrt(N); i++){
// Condition to check whether i
// is factor of N or not
if (N % i == 0){
long a = i ;
long b = N / i;
// Condition to check whether
// factors satisfies the
// equation or not
if ((b - a != 0) && ((b - a) % 2 == 0)){
// Stores minimum value
X = Math.min(X, (b - a) / 2) ;
}
}
}
// Return if X * X if X is not equal
// to 1e9 else return -1
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
// Driver code
public static void main (String[] args){
long N = 3;
System.out.println(SmallestPerfectSquare(N)) ;
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 code to find out the smallest
# perfect square X which when added to N
# yields another perfect square number.
import math
def SmallestPerfectSquare(N):
# X is the smallest perfect
# square number
X = 1e9
# Loop from 1 to square root of N
for i in range(1, int(math.sqrt(N)) + 1):
# Condition to check whether i
# is factor of N or not
if N % i == 0:
a = i
b = N // i
# Condition to check whether
# factors satisfies the
# equation or not
if b - a != 0 and (b - a) % 2 == 0:
# Stores minimum value
X = min(X, (b - a) // 2)
# Return if X * X if X is not equal
# to 1e9 else return -1
return(X * X if X != 1e9 else -1)
# Driver code
if __name__ == "__main__" :
N = 3
print(SmallestPerfectSquare(N))
C#
// C# code to find out the smallest
// perfect square X which when added to N
// yields another perfect square number.
using System;
class GFG {
static long SmallestPerfectSquare(long N)
{
// X is the smallest perfect
// square number
long X = (long)1e9;
long ans;
// Loop from 1 to square root of N
for(int i = 1; i < Math.Sqrt(N); i++){
// Condition to check whether i
// is factor of N or not
if (N % i == 0)
{
long a = i;
long b = N / i;
// Condition to check whether
// factors satisfies the
// equation or not
if ((b - a != 0) && ((b - a) % 2 == 0))
{
// Stores minimum value
X = Math.Min(X, (b - a) / 2);
}
}
}
// Return if X*X if X is not equal
// to 1e9 else return -1
if (X != 1e9)
ans = X * X;
else
ans = -1;
return ans;
}
// Driver code
public static void Main (string[] args)
{
long N = 3;
Console.WriteLine(SmallestPerfectSquare(N));
}
}
// This code is contributed by AnkitRai01
Javascript
输出:
1
时间复杂度: sqrt(N)