public class SommaDiDueCubi { public static void main(String[] args) { if (args.length != 1) { System.out.println("Uso: java SommaDiDueCubi "); return; } long n; try { n = Long.parseLong(args[0]); } catch (NumberFormatException e) { System.out.println("Errore: inserire un intero valido."); return; } for (long a = 1; a <= n; a++) { for (long b = a + 1; b <= n; b++) { long somma1 = (long)a * a * a + (long)b * b * b; if (somma1 > n) continue; for (long c = 1; c <= n; c++) { for (long d = c + 1; d <= n; d++) { // Evita che le combinazioni siano le stesse (a,b) = (c,d) if ((a == c && b == d) || (a == d && b == c)) continue; long somma2 = (long)c * c * c + (long)d * d * d; if (somma1 == somma2) { // Ordina le coppie (a,b) e (c,d) per evitare inversioni if (a < c || (a == c && b < d)) { System.out.println(somma1 + " = " + a + "^3 + " + b + "^3 = " + c + "^3 + " + d + "^3"); } } } } } } } }