File size: 2,809 Bytes
b8f9934
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Reference Sage/Python implementation for the four-cubes Pell search.

Run with:
    sage -python scripts/four_cubes_reference.py

or pass targets explicitly:
    sage -python scripts/four_cubes_reference.py 254 314159265358979323
"""

import sys

from sage.all import ZZ, BinaryQF, Mod, factor, is_square


def local_admissible(n, d, M):
    """Return whether the local square tests from Lemma 3.3 pass."""
    for p, a in factor(d):
        q = p**a
        if not Mod(M, q).is_square():
            return False
    return True


def solve_pell_with_congruences(d, M):
    """Find Pell candidates satisfying the required parity conditions."""
    Q = BinaryQF([1, 0, -d])
    for u, v in Q.solve_integer(M, _flag=3):
        u, v = ZZ(u), ZZ(v)
        if (u - 1) % 2 == 0 and (v - d) % 2 == 0:
            yield u, v


def quadruple_from_pell(d, u, v):
    """Convert a parity-compatible Pell solution to a four-cube tuple."""
    x = (u - 1) // 2
    y = (v - d) // 2
    return (x + 1, -x, -(y + d), y)


def height(xs):
    """Return H=max_i |x_i|."""
    return max(abs(x) for x in xs)


def solve(n, limit=10**8, return_d=False):
    """Return one quadruple whose cubes sum to n, or None."""
    n = ZZ(n)
    if n % 9 == 4:
        sol = solve(-n, limit=limit, return_d=True)
        if sol is None:
            return None
        x1, x2, x3, x4, d = sol
        out = (-x1, -x2, -x3, -x4)
        return out + (d,) if return_d else out

    residue = ZZ((1 - n) % 6)
    if residue == 0:
        residue = ZZ(6)

    for d in range(residue, limit + 1, 6):
        d = ZZ(d)
        if is_square(d):
            continue

        numerator = 4*n - 1 + d**3
        if numerator % 3 != 0:
            continue
        M = numerator // 3

        if not local_admissible(n, d, M):
            continue

        candidates = [
            quadruple_from_pell(d, u, v)
            for u, v in solve_pell_with_congruences(d, M)
        ]
        if not candidates:
            continue

        out = min(
            candidates,
            key=lambda xs: (height(xs), tuple(xs)),
        )
        return out + (d,) if return_d else out

    return None


def verify(n, xs):
    """Check x1^3 + x2^3 + x3^3 + x4^3 = n exactly."""
    return sum(ZZ(x)**3 for x in xs) == ZZ(n)


def main(argv):
    targets = [ZZ(arg) for arg in argv]
    if not targets:
        targets = [
            ZZ(254),
            ZZ(314159265358979323),
            ZZ(27182818284590452),
        ]

    for n in targets:
        sol = solve(n, limit=2000, return_d=True)
        ok = sol is not None and verify(n, sol[:4])
        print(f"n = {n}")
        print(f"solution = {sol}")
        print(f"verified = {ok}")
        if not ok:
            raise SystemExit(1)


if __name__ == "__main__":
    main(sys.argv[1:])