Four squared sum theorem, also known as Lagrange's theorem:
Each positive integer can be represented as a sum of squares of up to 4 positive integers.
If 0 is included, it can be represented as a sum of 4 squares.
Like what:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^ symbol denotes the meaning of a exponentiation)
For a given positive integer, there may be multiple representations of the sum of squares.
Ask you to sort 4 numbers:
0 <= a <= b <= c <= D
And for all possible representations press a,b,c,d for the combined primary key in ascending order, and finally output the first notation
Program input as a positive integer N (n<5000000)
Requires output of 4 non-negative integers, sorted from small to large, separated by spaces in the middle
For example, enter:
5
The program should output:
0 0 1 2
Again, for example, enter:
12
The program should output:
0 2 2 2
Again, for example, enter:
773535
The program should output:
1 1 267 838
Resource contract:
Peak memory consumption (with virtual machines) < 256M
CPU Consumption < 3000ms
Please strictly according to the requirements of the output, do not use the superfluous printing similar: "Please enter ..." Redundant content.
All the code is placed in the same source file, after debugging passed, the copy is submitted to the source.
Note: Do not use the package statement. Do not use jdk1.7 and the features of the above version.
Note: The name of the main class must be: main, otherwise it will be treated as invalid code.
There seems to be no easy way to go, just use the For loop.
PackageFour_squares;ImportJava.util.Scanner; Public classMain { Public Static voidMain (String args[]) {intA,b,c,d,n; Scanner Scanner=NewScanner (system.in); BooleanFlag =true; N=Scanner.nextint (); for(a=0;a<n&&flag;++a) for(b=a;b<n&&flag;++b) for(c=b;c<n&&flag;++c) for(d=c;d<n&&flag;d++){ if(n==a*a+b*b+c*c+d*d) {System.out.print (a+ "+ B +" "+ C +" "+d); Flag=false; } } }}
The four squares of the daily algorithm