Cristiano Magro
2025-10-04 851e6f15b30ddc7636cfe78d85a9b399f4e0d7b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class SommaDiDueCubi {
    public static void main(String[] args) {
        if (args.length != 1) {
            System.out.println("Uso: java SommaDiDueCubi <n>");
            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");
                            }                        }
                    }
                }
            }
        }
    }
}