//Lagrange.java // every natural number is the sum of four squares (can incl. 0) //e.g. 45 = 0^2+2^2+4^2+5^2 also 0^2+0^2+3^2+6^2 import java.awt.*; import javax.swing.*; public class Lagrange { public static void main (String[] args) { String input; int maxTry; int a=0, b=0, c=0, d=0; input = JOptionPane.showInputDialog("Max range of integers","100"); maxTry = Integer.parseInt(input); for (int n=1; n<=maxTry; n++) { nextN: for (a=0; a<=Math.sqrt(n); a++) { for (b=0; b<=Math.sqrt(n); b++) for (c=0; c<=Math.sqrt(n); c++) for (d=0; d<=Math.sqrt(n); d++) //maybe a better way exists... if (a*a+b*b+c*c+d*d == n) break nextN; } System.out.println(""+n+" "+a+" "+b+" "+c+" "+d+ " "+(a*a+b*b+c*c+d*d)); if (a*a+b*b+c*c+d*d != n) System.out.println("ERROR!"); } } }