C++ proves the four-way theorem

Four Square Theorem

The "Four Square Theorem" is a well-known theorem in number theory, which means that all natural numbers can be represented by at most the sum of the squares of four numbers.

prove

#include <iostream>
using namespace std;
 
int main() {
    
    
	long n, a, b, c, d;
	cout << "请输入一个数:" << endl;
	cin >> n; 
	for (a = 1; a <= n; a++) {
    
    
		for (b = 1; b <= a; b++) {
    
    
			for (c = 1; c <= b; c++) {
    
    
				for (d = 1; d <= c; d++) {
    
    
					if ((a * a + b * b + c * c + d * d) == n) {
    
    
						cout << a << "*" << a << "+" << b << "*" << b << "+" << c << "*" << c << "+" << d << "*" << d << "=" << n <<endl;
						break;
					}
				}
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_59197425/article/details/127676852