
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Lagrange’s four square theorem in C++
In this tutorial, we are going to learn about largrange's four square theorem.
The lagranges's four square theorem states that every natural number can be written as sum of squares of 4 numbers.
The following code finds the 4 numbers which satisfies the above condition for the given number n.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void printSquareCombinations(int n) { for (int i = 0; i * i <= n; i++) { for (int j = i; j * j <= n; j++) { for (int k = j; k * k <= n; k++) { for (int l = k; l * l <= n; l++) { if (i * i + j * j + k * k + l * l == n) { cout << n << " = " << i << "*" << i << " + " << j << "*" << j << " + " << k << "*" << k << " + "<< l << "*" << l << endl; } } } } } } int main() { int n = 25; printSquareCombinations(n); return 0; }
Output
If you run the above code, then you will get the following result.
25 = 0*0 + 0*0 + 0*0 + 5*5 25 = 0*0 + 0*0 + 3*3 + 4*4 25 = 1*1 + 2*2 + 2*2 + 4*4
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Euler's Four Square Identity in C++
- Lagrange Interpolation
- Kirchoff's Theorem
- Extended Midy's theorem in C++
- Fermat's Last Theorem in C++
- Fermat's little theorem in C++
- C++ Program for Zeckendorf's Theorem?
- Check if given four points form a Square
- C++ program to implement Inverse Interpolation using Lagrange Formula
- Find the greatest four digit number which is a perfect square.
- Find the square root of 12.0068 correct to four decimal places.
- Midy’s theorem in C++
- Carnots Theorem
- Find the least number with four digits which is a perfect square
- Find the least number of four digits which is a perfect square.

Advertisements