Java algorithm seventh session Blue Bridge Cup Group B (question + answer) 8. Four square sum

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.