fork(5) download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) {
  5. long number = 9223372036854775807L;
  6. LinkedList<Long> solution = new LinkedList<Long>();
  7. if (fourSquares(number, solution))
  8. System.out.println(number + " = sum of squares of: " + solution);
  9. else
  10. System.out.println(number + " could not be solved"); // never happens
  11. }
  12.  
  13. public static boolean fourSquares(long number, LinkedList<Long> solution) {
  14. if (number == 0) {
  15. for (long i = solution.size(); i < 4; i++)
  16. solution.add(0L);
  17. return true;
  18. }
  19.  
  20. if (solution.size() == 4)
  21. return false;
  22.  
  23. for (long i = (long) Math.sqrt(number); i > 0; i--) {
  24. solution.add(i);
  25. if (fourSquares(number - i * i, solution))
  26. return true;
  27. solution.removeLast();
  28. }
  29.  
  30. return false;
  31. }
  32. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
9223372036854775807 = sum of squares of: [3037000499, 76994, 671, 23]