8. Four Square sum (program design)
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.
1 public class_8 Four square and {2 public Static voidmain (string[] Args) {3Scanner Scanner =NewScanner (system.in);4 intn =Scanner.nextint ();5 intA = (int) (math.sqrt (n) +1);6 for(inti = 0; I < a; i++) {7 for(intj = i; J < a; J + +) {8 for(intK = j; K < a; k++) {9 intm = (int) Math.sqrt (n-i*i-j*j-k*k);Ten if(i*i+j*j+k*k+m*m==N) { oneSystem.out.println (i+ "" +j+ "" +k+ "" +m); a return; - } - } the } - } - } -}
Java algorithm seventh session Blue Bridge Cup Group B (question + answer) 8. Four square sum