Added Sum of Squares Lagrange's Four Square algorithm implementation (#6583)

* Add Sum of Squares algorithm implementation

* Format code and add Wikipedia URL for Lagrange's theorem

* Fixed clang-format issues

---------

Co-authored-by: Oleksandr Klymenko <alexanderklmn@gmail.com>
This commit is contained in:
Sriram kulkarni
2025-10-02 13:14:36 +05:30
committed by GitHub
parent 15695c6e54
commit d8ddb07b7e
2 changed files with 121 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package com.thealgorithms.maths;
/**
* Implementation of Lagrange's Four Square Theorem
* Find minimum number of perfect squares that sum to given number
*
* @see <a href="https://en.wikipedia.org/wiki/Lagrange%27s_four-square_theorem">Lagrange's Four Square Theorem</a>
* @author BEASTSHRIRAM
*/
public final class SumOfSquares {
private SumOfSquares() {
// Utility class
}
/**
* Find minimum number of perfect squares that sum to n
*
* @param n the target number
* @return minimum number of squares needed
*/
public static int minSquares(int n) {
if (isPerfectSquare(n)) {
return 1;
}
for (int i = 1; i * i <= n; i++) {
int remaining = n - i * i;
if (isPerfectSquare(remaining)) {
return 2;
}
}
// Legendre's three-square theorem
int temp = n;
while (temp % 4 == 0) {
temp /= 4;
}
if (temp % 8 == 7) {
return 4;
}
return 3;
}
private static boolean isPerfectSquare(int n) {
if (n < 0) {
return false;
}
int root = (int) Math.sqrt(n);
return root * root == n;
}
}