先决条件:拉格朗日的四平方定理,勒让德的三平方定理
数字始终可以表示为其他数字的平方和。请注意,1是一个正方形,我们总是可以将数字打破为(1 * 1 + 1 * 1 + 1 * 1 +…)。给定一个数字n,求和为N的最小平方数。
例子:
Input: N = 13
Output: 2
Explanation:
13 can be expressed as, 13 = 32 + 22. Hence, the output is 2.
Input: N = 100
Output: 1
Explanation:
100 can be expressed as 100 = 102. Hence, the output is 1.
天真的方法:有关[O(N * sqrt(N))]方法,请参阅本文的Set 2 。
高效方法:要优化幼稚方法,该想法是使用拉格朗日的四平方定理和勒让德的三平方定理。下面讨论这两个定理:
Lagrange’s four-square theorem, also known as Bachet’s conjecture, states that every natural number can be represented as the sum of four integer squares, where each integer is non-negative.
Legendre’s three-square theorem states that a natural number can be represented as the sum of three squares of integers if and only if n is not of the form : n = 4a (8b+7), for non-negative integers a and b.
因此,证明了代表任何数字N的最小平方数只能在集合{1,2,3,4}内。因此,仅检查这四个可能的值,就可以找到代表任何数字N的最小平方数。请按照以下步骤操作:
- 如果N是一个完美的平方,则结果为1 。
- 如果N可以表示为两个平方的和,则结果为2 。
- 如果N不能以N = 4 a (8b + 7)的形式表示,其中a和b是非负整数,则根据勒让德三平方定理,结果为3 。
- 如果不满足上述所有条件,则根据拉格朗日的四平方定理,结果为4 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function that returns true if N
// is a perfect square
bool isPerfectSquare(int N)
{
int floorSqrt = sqrt(N);
return (N == floorSqrt * floorSqrt);
}
// Function that returns true check if
// N is sum of three squares
bool legendreFunction(int N)
{
// Factor out the powers of 4
while (N % 4 == 0)
N /= 4;
// N is NOT of the
// form 4^a * (8b + 7)
if (N % 8 != 7)
return true;
else
return false;
}
// Function that finds the minimum
// number of square whose sum is N
int minSquares(int N)
{
// If N is perfect square
if (isPerfectSquare(N))
return 1;
// If N is sum of 2 perfect squares
for (int i = 1; i * i < N; i++) {
if (isPerfectSquare(N - i * i))
return 2;
}
// If N is sum of 3 perfect squares
if (legendreFunction(N))
return 3;
// Otherwise, N is the
// sum of 4 perfect squares
return 4;
}
// Driver code
int main()
{
// Given number
int N = 123;
// Function call
cout << minSquares(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function that returns true if N
// is a perfect square
static boolean isPerfectSquare(int N)
{
int floorSqrt = (int)Math.sqrt(N);
return (N == floorSqrt * floorSqrt);
}
// Function that returns true check if
// N is sum of three squares
static boolean legendreFunction(int N)
{
// Factor out the powers of 4
while (N % 4 == 0)
N /= 4;
// N is NOT of the
// form 4^a * (8b + 7)
if (N % 8 != 7)
return true;
else
return false;
}
// Function that finds the minimum
// number of square whose sum is N
static int minSquares(int N)
{
// If N is perfect square
if (isPerfectSquare(N))
return 1;
// If N is sum of 2 perfect squares
for(int i = 1; i * i < N; i++)
{
if (isPerfectSquare(N - i * i))
return 2;
}
// If N is sum of 3 perfect squares
if (legendreFunction(N))
return 3;
// Otherwise, N is the
// sum of 4 perfect squares
return 4;
}
// Driver code
public static void main(String[] args)
{
// Given number
int N = 123;
// Function call
System.out.print(minSquares(N));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
from math import sqrt, floor, ceil
# Function that returns True if N
# is a perfect square
def isPerfectSquare(N):
floorSqrt = floor(sqrt(N))
return (N == floorSqrt * floorSqrt)
# Function that returns True check if
# N is sum of three squares
def legendreFunction(N):
# Factor out the powers of 4
while (N % 4 == 0):
N //= 4
# N is NOT of the
# form 4^a * (8b + 7)
if (N % 8 != 7):
return True
else:
return False
# Function that finds the minimum
# number of square whose sum is N
def minSquares(N):
# If N is perfect square
if (isPerfectSquare(N)):
return 1
# If N is sum of 2 perfect squares
for i in range(N):
if i * i < N:
break
if (isPerfectSquare(N - i * i)):
return 2
# If N is sum of 3 perfect squares
if (legendreFunction(N)):
return 3
# Otherwise, N is the
# sum of 4 perfect squares
return 4
# Driver code
if __name__ == '__main__':
# Given number
N = 123
# Function call
print(minSquares(N))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function that returns true if N
// is a perfect square
static bool isPerfectSquare(int N)
{
int floorSqrt = (int)Math.Sqrt(N);
return (N == floorSqrt * floorSqrt);
}
// Function that returns true check
// if N is sum of three squares
static bool legendreFunction(int N)
{
// Factor out the powers of 4
while (N % 4 == 0)
N /= 4;
// N is NOT of the
// form 4^a * (8b + 7)
if (N % 8 != 7)
return true;
else
return false;
}
// Function that finds the minimum
// number of square whose sum is N
static int minSquares(int N)
{
// If N is perfect square
if (isPerfectSquare(N))
return 1;
// If N is sum of 2 perfect squares
for(int i = 1; i * i < N; i++)
{
if (isPerfectSquare(N - i * i))
return 2;
}
// If N is sum of 3 perfect squares
if (legendreFunction(N))
return 3;
// Otherwise, N is the
// sum of 4 perfect squares
return 4;
}
// Driver code
public static void Main(String[] args)
{
// Given number
int N = 123;
// Function call
Console.Write(minSquares(N));
}
}
// This code is contributed by Rajput-Ji
3
时间复杂度: O(sqrt(N))
辅助空间: O(1)