1-- Sums of powers: closed forms for the sums of the first n squares and cubes,2-- and the sum of cubes as the square of the sum of the first n naturals3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Real.PowerSums9import ArithmeticSeries1011-- The squares and cubes of the naturals, as real sequences.12defgraph squareTerms (n ∈ ℕ) : ℝ := (realNat(n):R) * (realNat(n):R)13proof14have typedN := nat.omega.backward at n with member15have count := realNat.real at n with typedN16exact realMulValue.real at realNat(n), realNat(n)1718defgraph cubeTerms (n ∈ ℕ) : ℝ := (realNat(n):R) * (realNat(n):R) * (realNat(n):R)19proof20have typedN := nat.omega.backward at n with member21have count := realNat.real at n with typedN22have square := realMulValue.real at realNat(n), realNat(n)23exact realMulValue.real at (realNat(n):R) * (realNat(n):R), realNat(n)2425theorem squareTerms.sequence26shows RealSequence(squareTerms)27proof28exact squareTerms.function2930theorem cubeTerms.sequence31shows RealSequence(cubeTerms)32proof33exact cubeTerms.function3435-- 6·(0² + 1² + ⋯ + (n−1)²) = n(n − 1)(2n − 1).36theorem powerSum.squares (n : Nat)37shows (6:R) * (∑ i < n, squareTerms(i):R) =38 (realNat(n):R) * ((realNat(n):R) - (1:R)) * ((2:R) * (realNat(n):R) - (1:R))39proof40induction n41| zero =>42rw [realPartialSum.zero, realNat.zero]43ring44| succ(k) using ih =>45have sequence := squareTerms.sequence46have count := realNat.real at k47have value := squareTerms.value at k with*48rw [realPartialSum.succ, value, realNat.succ]49linear_combination ih5051-- 6·(1² + ⋯ + n²) = n(n + 1)(2n + 1).52theorem powerSum.squares_inclusive (n : Nat)53shows (6:R) * (∑ i < n +1, squareTerms(i):R) =54 (realNat(n):R) * ((realNat(n):R) + (1:R)) * ((2:R) * (realNat(n):R) + (1:R))55proof56have bound : n +1= succ(n) := add.right_one at n57rw bound58have all := powerSum.squares at succ(n)59rw realNat.succ at all60linear_combination all6162-- 4·(0³ + 1³ + ⋯ + (n−1)³) = n²(n − 1)².63theorem powerSum.cubes (n : Nat)64shows (4:R) * (∑ i < n, cubeTerms(i):R) =65 (realNat(n):R) * (realNat(n):R) * ((realNat(n):R) - (1:R)) * ((realNat(n):R) - (1:R))66proof67induction n68| zero =>69rw [realPartialSum.zero, realNat.zero]70ring71| succ(k) using ih =>72have sequence := cubeTerms.sequence73have count := realNat.real at k74have value := cubeTerms.value at k with*75rw [realPartialSum.succ, value, realNat.succ]76linear_combination ih7778-- 4·(1³ + ⋯ + n³) = n²(n + 1)².79theorem powerSum.cubes_inclusive (n : Nat)80shows (4:R) * (∑ i < n +1, cubeTerms(i):R) =81 (realNat(n):R) * (realNat(n):R) * ((realNat(n):R) + (1:R)) * ((realNat(n):R) + (1:R))82proof83have bound : n +1= succ(n) := add.right_one at n84rw bound85have all := powerSum.cubes at succ(n)86rw realNat.succ at all87linear_combination all8889-- Nicomachus: 1³ + ⋯ + n³ = (1 + ⋯ + n)².90theorem powerSum.cubes_eq_square_of_sum (n : Nat)91shows ∑ i < n +1, cubeTerms(i) =92 (∑ i < n +1, arithmeticTerms(realZero, realOne)(i):R) *93 (∑ i < n +1, arithmeticTerms(realZero, realOne)(i):R)94proof95have bound : n +1= succ(n) := add.right_one at n96rw bound97have cubes := powerSum.cubes_inclusive at n98rw bound at cubes99have gauss := arithmeticSeries.gauss at n100rw bound at gauss101have scaled : (4:R) * ((∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R)) = (0:R)102linear_combination cubes - ((2:R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) + (realNat(n):R) * ((realNat(n):R) + (1:R))) * gauss103have fourPositive : (0:R) < (4:R) bylinarith104have fourNonzero : (4:R) ≠ (0:R)105intro zero106rw zero at fourPositive107linarith108have cancel := realInvValue.mul_left with fourNonzero109have identity : (∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) = realInvValue((4:R)) * ((4:R) * ((∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R)))110have rearrange : realInvValue((4:R)) * ((4:R) * ((∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R))) = (realInvValue((4:R)) * (4:R)) * ((∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R))111ring112have one := realMulValue.one_left at (∑ i < succ(n), cubeTerms(i):R) - (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R) * (∑ i < succ(n), arithmeticTerms(realZero, realOne)(i):R)113rw [rearrange, cancel, one]114rfl115rw scaled at identity116have zero := realMulValue.zero_right at realInvValue((4:R))117rw zero at identity118linear_combination identity119