Thursday, June 27, 2024
HomeData ModellingData Structure & AlgorithmLagrange’s four square theorem

Lagrange’s four square theorem

Lagrange’s Four Square Theorem states that every natural number can be written as sum of squares of four non negative integers.

For eg. 1 = 0^2 + 0^2 + 0^2 +1^2
Similarly 2 = 0^2 + 0^2 + 1^2 +1^2
Similarly for any 
n\in N, n = a^2 + b^2 + c^2 + d^2 where \ a, b, c, d, \in N \cup \{0\}

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.
?>


Javascript




<script>
 
// Javascript 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(let i = 0; i * i <= a; i++)
    {
        for(let j = i; j * j <= a; j++)
        {
            for(let k = j; k * k <= a; k++) 
            {
                for(let 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
                        document.write(a + " = " + i + "*" +
                                       i + " + " + j + "*" +
                                       j + " + ");
                        document.write(k + "*" + k + " + " +
                                       l + "*" + l + "</br>");
                    }
                }
            }
        }
    }
}
 
// Driver code
let 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 decode2207
 
</script>


Output: 

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

 

Time complexity : O(a2
Auxiliary Space : O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments