All Euler problems
Project Euler

The Ackermann Function

Define A(n) = ack(n, n) where ack is the Ackermann function: ack(0, m) = m + 1, ack(n, 0) = ack(n-1, 1) for n > 0, ack(n, m) = ack(n-1, ack(n, m-1)) for n, m > 0. Find sum_(n=0)^6 A(n) (mod 14^8).

Source sync Apr 19, 2026
Problem #0282
Level Level 14
Solved By 1,218
Languages C++, Python
Answer 1098988351
Length 412 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

For non-negative integers \(m\), \(n\), the Ackermann function \(A(m,n)\) is defined as follows: \begin {align*} A(m, n) = \begin {cases} n + 1 & \text {if } m = 0 \\ A(m - 1, 1) & \text {if } m > 0 \text { and } n = 0 \\ A(m - 1, A(m, n - 1)) & \text {if } m > 0 \text { and } n > 0 \end {cases} \end {align*}

For example \(A(1,0) = 2\), \(A(2,2) = 7\) and \(A(3,4) = 125\).

Find \(\displaystyle \sum _{n=0}^6 A(n,n)\) and give your answer mod \(14^8\).

Problem 282: The Ackermann Function

Mathematical Development

Theorem 1 (Closed Forms for Ackermann Levels 0—4)

Theorem. For all m0m \ge 0:

  1. ack(0,m)=m+1\operatorname{ack}(0, m) = m + 1.
  2. ack(1,m)=m+2\operatorname{ack}(1, m) = m + 2.
  3. ack(2,m)=2m+3\operatorname{ack}(2, m) = 2m + 3.
  4. ack(3,m)=2m+33\operatorname{ack}(3, m) = 2^{m+3} - 3.
  5. ack(4,m)=222m+33=m+323\operatorname{ack}(4, m) = \underbrace{2^{2^{\cdot^{\cdot^{\cdot^2}}}}}_{m+3} - 3 = {}^{m+3}2 - 3.

Proof. Each level is established by induction on mm.

(1) By definition.

(2) Base case: ack(1,0)=ack(0,1)=2=0+2\operatorname{ack}(1,0) = \operatorname{ack}(0,1) = 2 = 0 + 2. Inductive step: ack(1,m)=ack(0,ack(1,m1))=ack(1,m1)+1\operatorname{ack}(1,m) = \operatorname{ack}(0, \operatorname{ack}(1, m-1)) = \operatorname{ack}(1,m-1) + 1. By the inductive hypothesis ack(1,m1)=m+1\operatorname{ack}(1,m-1) = m+1, so ack(1,m)=m+2\operatorname{ack}(1,m) = m+2.

(3) Base case: ack(2,0)=ack(1,1)=3=20+3\operatorname{ack}(2,0) = \operatorname{ack}(1,1) = 3 = 2 \cdot 0 + 3. Inductive step: ack(2,m)=ack(1,ack(2,m1))=ack(2,m1)+2=(2(m1)+3)+2=2m+3\operatorname{ack}(2,m) = \operatorname{ack}(1, \operatorname{ack}(2,m-1)) = \operatorname{ack}(2,m-1) + 2 = (2(m-1)+3) + 2 = 2m+3.

(4) Base case: ack(3,0)=ack(2,1)=5=233\operatorname{ack}(3,0) = \operatorname{ack}(2,1) = 5 = 2^3 - 3. Inductive step: ack(3,m)=ack(2,ack(3,m1))=2ack(3,m1)+3=2(2m+23)+3=2m+33\operatorname{ack}(3,m) = \operatorname{ack}(2, \operatorname{ack}(3,m-1)) = 2\operatorname{ack}(3,m-1) + 3 = 2(2^{m+2}-3)+3 = 2^{m+3}-3.

(5) Base case: ack(4,0)=ack(3,1)=243=13=323\operatorname{ack}(4,0) = \operatorname{ack}(3,1) = 2^4 - 3 = 13 = {}^{3}2 - 3. Inductive step: ack(4,m)=ack(3,ack(4,m1))=2ack(4,m1)+33=2m+223=m+323\operatorname{ack}(4,m) = \operatorname{ack}(3, \operatorname{ack}(4,m-1)) = 2^{\operatorname{ack}(4,m-1)+3} - 3 = 2^{{}^{m+2}2} - 3 = {}^{m+3}2 - 3. \qquad \blacksquare

Corollary. The diagonal values are: A(0)=1A(0) = 1, A(1)=3A(1) = 3, A(2)=7A(2) = 7, A(3)=61A(3) = 61, A(4)=723A(4) = {}^{7}2 - 3.

Theorem 2 (Modular Stabilization of Iterated Exponentiation)

Theorem. For any positive integer MM, there exists a non-negative integer HH such that h2H2(modM){}^{h}2 \equiv {}^{H}2 \pmod{M} for all hHh \ge H. The minimal such HH equals the length of the Carmichael chain M,λ(M),λ(λ(M)),,1M, \lambda(M), \lambda(\lambda(M)), \ldots, 1.

Proof. By strong induction on MM.

Base case: M=1M = 1. Then H=0H = 0 and the congruence h20(mod1){}^{h}2 \equiv 0 \pmod{1} holds trivially.

Inductive step: Let M>1M > 1.

Case 1: gcd(2,M)=1\gcd(2, M) = 1. By the generalized Euler theorem, 2a2b(modM)2^a \equiv 2^b \pmod{M} whenever ab(modλ(M))a \equiv b \pmod{\lambda(M)}, where λ\lambda is Carmichael’s function. Since λ(M)<M\lambda(M) < M, the inductive hypothesis applies to λ(M)\lambda(M): the sequence h12modλ(M){}^{h-1}2 \bmod \lambda(M) stabilizes for h1Hh - 1 \ge H', where HH' is the chain length for λ(M)\lambda(M). Hence h2=2h12modM{}^{h}2 = 2^{{}^{h-1}2} \bmod M stabilizes for hH+1h \ge H' + 1.

Case 2: 2M2 \mid M. Write M=2aMM = 2^a \cdot M' with gcd(2,M)=1\gcd(2, M') = 1 and a1a \ge 1. For hah \ge a, the tower h2{}^{h}2 is divisible by 2a2^a, so h20(mod2a){}^{h}2 \equiv 0 \pmod{2^a}. By Case 1, h2modM{}^{h}2 \bmod M' stabilizes. By the Chinese Remainder Theorem, h2modM{}^{h}2 \bmod M stabilizes. \qquad \blacksquare

Theorem 3 (Computation via CRT Decomposition)

Theorem. Let M=148=2878M = 14^8 = 2^8 \cdot 7^8. Then:

  1. For k4k \ge 4: A(k)3253(mod256)A(k) \equiv -3 \equiv 253 \pmod{256}, since the tower h2{}^{h}2 satisfies h20(mod256){}^{h}2 \equiv 0 \pmod{256} for all h8h \ge 8, and the tower heights in A(k)A(k) for k4k \ge 4 exceed 88.
  2. Modulo 787^8: since gcd(2,78)=1\gcd(2, 7^8) = 1, we compute via the iterated Carmichael chain. For A(4)=723A(4) = {}^{7}2 - 3, we evaluate 72mod78{}^{7}2 \bmod 7^8 by recursing down the chain. For A(5)A(5) and A(6)A(6), the tower heights vastly exceed the chain length, so the modular residue has stabilized to the same value as 2mod78{}^{\infty}2 \bmod 7^8.
  3. The final values are combined via CRT to obtain A(k)mod148A(k) \bmod 14^8 for each k{4,5,6}k \in \{4, 5, 6\}.

Proof. Part (1): h2=2h12{}^{h}2 = 2^{{}^{h-1}2}, and for h2h \ge 2, the exponent h124>8{}^{h-1}2 \ge 4 > 8 is false only for small hh; checking, 22=4{}^{2}2 = 4, 32=16{}^{3}2 = 16, 42=65536{}^{4}2 = 65536. For h4h \ge 4, the exponent exceeds 88, giving 2(exponent)0(mod28)2^{(\text{exponent})} \equiv 0 \pmod{2^8}. For A(4)A(4), the tower height is 747 \ge 4, so 720(mod256){}^{7}2 \equiv 0 \pmod{256} and A(4)3(mod256)A(4) \equiv -3 \pmod{256}.

Part (2): The Carmichael chain for 787^8 is 78λ(78)=677λ(677)=lcm(λ(2),λ(3),λ(77))=lcm(1,2,676)=6767^8 \to \lambda(7^8) = 6 \cdot 7^7 \to \lambda(6 \cdot 7^7) = \operatorname{lcm}(\lambda(2), \lambda(3), \lambda(7^7)) = \operatorname{lcm}(1, 2, 6 \cdot 7^6) = 6 \cdot 7^6 \to \cdots, terminating in O(logM)O(\log M) steps. For A(5)A(5): ack(5,0)=65533\operatorname{ack}(5,0) = 65533 and ack(5,1)=6553623\operatorname{ack}(5,1) = {}^{65536}2 - 3, where the tower height 6553665536 already exceeds any conceivable chain length. For subsequent values of ack(5,m)\operatorname{ack}(5, m) and all values of ack(6,m)\operatorname{ack}(6, m), the tower heights grow hyper-exponentially and yield the same stabilized residue.

Part (3): Standard CRT reconstruction from the residues modulo 282^8 and 787^8. \qquad \blacksquare

Editorial

Closed forms: A(0)=1, A(1)=3, A(2)=7, A(3)=61, A(4)=2^^7 - 3. A(5) and A(6) involve towers that stabilize modulo 14^8. Computation via CRT (mod 2^8 and mod 7^8) with iterated Carmichael lambda. We compute A(4), A(5), A(6) mod M via tower2_mod. We then compute 2^^height mod m via CRT on 2-part and odd-part. Finally, recurse using Carmichael’s lambda for the odd part.

Pseudocode

Compute A(4), A(5), A(6) mod M via tower2_mod
Compute 2^^height mod m via CRT on 2-part and odd-part
Recurse using Carmichael's lambda for the odd part

Complexity Analysis

  • Time: O(LlogM)O(L \cdot \log M) where L=O(logM)L = O(\log M) is the Carmichael chain length. Each level involves one modular exponentiation costing O(logM)O(\log M) multiplications. Total: O(log2M)O(\log^2 M).
  • Space: O(L)=O(logM)O(L) = O(\log M) for the recursion stack.

Answer

1098988351\boxed{1098988351}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_282/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128 lll;

ll powmod(ll a, ll b, ll m) {
    if (m == 1) return 0;
    a %= m;
    if (a < 0) a += m;
    ll result = 1;
    while (b > 0) {
        if (b & 1) result = (lll)result * a % m;
        a = (lll)a * a % m;
        b >>= 1;
    }
    return result;
}

ll euler_phi(ll n) {
    ll result = n;
    for (ll p = 2; p * p <= n; p++) {
        if (n % p == 0) {
            while (n % p == 0) n /= p;
            result -= result / p;
        }
    }
    if (n > 1) result -= result / n;
    return result;
}

ll carmichael(ll n) {
    ll result = 1;
    for (ll p = 2; p * p <= n; p++) {
        if (n % p == 0) {
            int e = 0;
            ll pk = 1;
            while (n % p == 0) { n /= p; e++; pk *= p; }
            ll lam = (p == 2 && e >= 3) ? pk / 4 : pk - pk / p;
            result = lcm(result, lam);
        }
    }
    if (n > 1) result = lcm(result, n - 1);
    return result;
}

// Compute 2^^height mod m via CRT on 2-part and odd-part
pair<ll, bool> tower2_mod(int height, ll m) {
    if (m == 1) return {0, true};
    if (height == 0) return {1 % m, 1 >= m};
    if (height == 1) return {2 % m, 2 >= m};

    int v2 = 0;
    ll modd = m;
    while (modd % 2 == 0) { modd /= 2; v2++; }

    // Mod 2^v2
    ll mod_2part = 0;
    if (v2 > 0) {
        ll exp_val;
        if (height - 1 <= 3) {
            ll small[] = {1, 2, 4, 16};
            exp_val = small[height - 1];
        } else {
            exp_val = 65536;
        }
        if (exp_val < v2)
            mod_2part = (1LL << exp_val) % (1LL << v2);
    }

    // Mod odd part
    ll mod_oddpart = 0;
    if (modd > 1) {
        ll lam = carmichael(modd);
        auto [exp_mod, _] = tower2_mod(height - 1, lam);
        mod_oddpart = powmod(2, exp_mod, modd);
    }

    // CRT
    ll result;
    if (v2 == 0) {
        result = mod_oddpart % m;
    } else {
        ll p2 = 1LL << v2;
        ll inv_p2 = powmod(p2 % modd, euler_phi(modd) - 1, modd);
        ll diff = ((mod_oddpart - mod_2part) % modd + modd) % modd;
        ll t = (lll)diff * inv_p2 % modd;
        result = mod_2part + p2 * t;
    }
    return {result % m, true};
}

int main() {
    ll M = 1;
    for (int i = 0; i < 8; i++) M *= 14;

    ll a0 = 1, a1 = 3, a2 = 7, a3 = 61;
    ll a4 = (tower2_mod(7, M).first - 3 + M) % M;
    ll stable = tower2_mod(100, M).first;
    ll a5 = (stable - 3 + M) % M;
    ll a6 = a5;

    cout << (a0 + a1 + a2 + a3 + a4 + a5 + a6) % M << endl;
    return 0;
}