Lagrange’s Four Square Theorem states that every natural number can be written as sum of squares of four non negative integers.
For eg.
Similarly
Similarly for any
The above identity may be derived from Euler’s four square identity: which says we can write a product of 2 numbers (which can be written as sum of 4 squares) as the sum of 4 squares.
C++
// CPP program for Lagrange's four square identity #include <bits/stdc++.h> using namespace std; // Prints all the possible combinations 4 numbers // whose sum of squares is equal to the given no. void printFourSquares(int a) { // loops checking the sum of squares for (int i = 0; i * i <= a; i++) { for (int j = i; j * j <= a; j++) { for (int k = j; k * k <= a; k++) { for (int l = k; l * l <= a; l++) { // if sum of four squares equals // the given no. if (i * i + j * j + k * k + l * l == a) { // printing the numbers cout << a << " = " << i << "*" << i << " + " << j << "*" << j << " + "; cout << k << "*" << k << " + " << l << "*" << l << "\n"; } } } } } } // Driver Code int main() { int a = 74; // 74 = 0*0 + 0*0 + 5*5 + 7*7 // 74 = 0*0 + 1*1 + 3*3 + 8*8 // 74 = 0*0 + 3*3 + 4*4 + 7*7 // 74 = 1*1 + 1*1 + 6*6 + 6*6 // 74 = 2*2 + 3*3 + 5*5 + 6*6 printFourSquares(a); return 0; } |
Java
// Java program for Lagrange's four square identity class GFG { // Prints all the possible combinations 4 numbers // whose sum of squares is equal to the given no. static void printFourSquares(int a) { // loops checking the sum of squares for (int i = 0; i * i <= a; i++) { for (int j = i; j * j <= a; j++) { for (int k = j; k * k <= a; k++) { for (int l = k; l * l <= a; l++) { // if sum of four squares equals // the given no. if (i * i + j * j + k * k + l * l == a) { // printing the numbers System.out.print( a + " = " + i + "*" + i + " + " + j + "*" + j + " + "); System.out.println( k + "*" + k + " + " + l + "*" + l); } } } } } } // Driver Code public static void main(String[] args) { int a = 74; // 74 = 0*0 + 0*0 + 5*5 + 7*7 // 74 = 0*0 + 1*1 + 3*3 + 8*8 // 74 = 0*0 + 3*3 + 4*4 + 7*7 // 74 = 1*1 + 1*1 + 6*6 + 6*6 // 74 = 2*2 + 3*3 + 5*5 + 6*6 printFourSquares(a); } } // This code is contributed by smitha |
Python3
# Python program for Lagrange's four square identity # Prints all the possible combinations 4 numbers # whose sum of squares is equal to the given no. def printFourSquares(a) : # loops checking the sum of squares i = 0 while (i * i <= a) : j = i while (j * j <= a) : k = j while (k * k <= a) : l = k while (l * l <= a) : # if sum of four squares equals # the given no. if (i * i + j * j + k * k + l * l == a) : # printing the numbers print ("{} = {}*{} + {}*{} +". format(a,i,i,j,j), end = " ") print ("{}*{} + {}*{}". format(k,k,l,l), end="\n") l = l + 1 k = k + 1 j = j + 1 i = i + 1 # Driver Code a = 74 # 74 = 0*0 + 0*0 + 5*5 + 7*7 # 74 = 0*0 + 1*1 + 3*3 + 8*8 # 74 = 0*0 + 3*3 + 4*4 + 7*7 # 74 = 1*1 + 1*1 + 6*6 + 6*6 # 74 = 2*2 + 3*3 + 5*5 + 6*6 printFourSquares(a) # This code is contributed by Manish Shaw # (manishshaw1) |
C#
// C# program for Lagrange's four square identity using System; class GFG { // Prints all the possible combinations 4 numbers // whose sum of squares is equal to the given no. static void printFourSquares(int a) { // loops checking the sum of squares for (int i = 0; i * i <= a; i++) { for (int j = i; j * j <= a; j++) { for (int k = j; k * k <= a; k++) { for (int l = k; l * l <= a; l++) { // if sum of four squares equals // the given no. if (i * i + j * j + k * k + l * l == a) { // printing the numbers Console.Write( a + " = " + i + "*" + i + " + " + j + "*" + j + " + "); Console.Write( k + "*" + k + " + " + l + "*" + l + "\n"); } } } } } } // Driver code public static void Main() { int a = 74; // 74 = 0*0 + 0*0 + 5*5 + 7*7 // 74 = 0*0 + 1*1 + 3*3 + 8*8 // 74 = 0*0 + 3*3 + 4*4 + 7*7 // 74 = 1*1 + 1*1 + 6*6 + 6*6 // 74 = 2*2 + 3*3 + 5*5 + 6*6 printFourSquares(a); } } // This code is contributed by Smitha |
PHP
<?php //PHP program for Lagrange's four square identity // Prints all the possible combinations 4 numbers // whose sum of squares is equal to the given no. function printFourSquares($a) { // loops checking the sum of squares for ($i = 0; $i * $i <= $a; $i++) { for ( $j = $i; $j * $j <= $a; $j++) { for ($k = $j; $k * $k <= $a; $k++) { for ($l = $k; $l * $l <= $a; $l++) { // if sum of four squares equals // the given no. if ($i * $i + $j * $j + $k * $k + $l * $l == $a) { // printing the numbers echo $a , " = " , $i , "*" ,$i, " + " , $j , "*" , $j , " + "; echo $k , "*" , $k , " + ", $l , "*" , $l , "\n"; } } } } } } // Driver Code $a = 74; // 74 = 0*0 + 0*0 + 5*5 + 7*7 // 74 = 0*0 + 1*1 + 3*3 + 8*8 // 74 = 0*0 + 3*3 + 4*4 + 7*7 // 74 = 1*1 + 1*1 + 6*6 + 6*6 // 74 = 2*2 + 3*3 + 5*5 + 6*6 printFourSquares($a); // This code is contributed by ajit. ?> |
74 = 0*0 + 0*0 + 5*5 + 7*7 74 = 0*0 + 1*1 + 3*3 + 8*8 74 = 0*0 + 3*3 + 4*4 + 7*7 74 = 1*1 + 1*1 + 6*6 + 6*6 74 = 2*2 + 3*3 + 5*5 + 6*6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to check if given four points form a square
- Euler's Four Square Identity
- Find Four points such that they form a square whose sides are parallel to x and y axes
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- Check if four segments form a rectangle
- Queries to find whether a number has exactly four distinct factors or not
- Number of digits in the nth number made of given four digits
- Find four factors of N with maximum product and sum equal to N | Set-2
- Find four factors of N with maximum product and sum equal to N
- Maximize the product of four factors of a Number
- Find four factors of N with maximum product and sum equal to N | Set 3
- Maximum possible time that can be formed from four digits
- Find the number of ways to divide number into four parts such that a = c and b = d
- Minimum sum obtained from groups of four elements from the given array
- Find the prime P using given four integers
- Number of ways N can be divided into four parts to construct a rectangle
- Swapping four variables without temporary variable
- Count square and non-square numbers before n
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Find smallest perfect square number A such that N + A is also a perfect square number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

