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).
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 :
- .
- .
- .
- .
- .
Proof. Each level is established by induction on .
(1) By definition.
(2) Base case: . Inductive step: . By the inductive hypothesis , so .
(3) Base case: . Inductive step: .
(4) Base case: . Inductive step: .
(5) Base case: . Inductive step: .
Corollary. The diagonal values are: , , , , .
Theorem 2 (Modular Stabilization of Iterated Exponentiation)
Theorem. For any positive integer , there exists a non-negative integer such that for all . The minimal such equals the length of the Carmichael chain .
Proof. By strong induction on .
Base case: . Then and the congruence holds trivially.
Inductive step: Let .
Case 1: . By the generalized Euler theorem, whenever , where is Carmichael’s function. Since , the inductive hypothesis applies to : the sequence stabilizes for , where is the chain length for . Hence stabilizes for .
Case 2: . Write with and . For , the tower is divisible by , so . By Case 1, stabilizes. By the Chinese Remainder Theorem, stabilizes.
Theorem 3 (Computation via CRT Decomposition)
Theorem. Let . Then:
- For : , since the tower satisfies for all , and the tower heights in for exceed .
- Modulo : since , we compute via the iterated Carmichael chain. For , we evaluate by recursing down the chain. For and , the tower heights vastly exceed the chain length, so the modular residue has stabilized to the same value as .
- The final values are combined via CRT to obtain for each .
Proof. Part (1): , and for , the exponent is false only for small ; checking, , , . For , the exponent exceeds , giving . For , the tower height is , so and .
Part (2): The Carmichael chain for is , terminating in steps. For : and , where the tower height already exceeds any conceivable chain length. For subsequent values of and all values of , the tower heights grow hyper-exponentially and yield the same stabilized residue.
Part (3): Standard CRT reconstruction from the residues modulo and .
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: where is the Carmichael chain length. Each level involves one modular exponentiation costing multiplications. Total: .
- Space: for the recursion stack.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Project Euler Problem 282: The Ackermann Function
Find sum_{n=0}^{6} A(n) mod 14^8 where A(n) = ack(n, n).
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.
"""
from sympy.ntheory import factorint
from math import gcd
def carmichael(n):
if n == 1:
return 1
result = 1
factors = factorint(n)
for p, e in factors.items():
if p == 2:
lam = 1 if e == 1 else (2 if e == 2 else p ** (e - 2))
else:
lam = (p - 1) * p ** (e - 1)
result = result * lam // gcd(result, lam)
return result
def tower2_mod(height, m):
if m == 1:
return 0
if height == 0:
return 1 % m
if height == 1:
return 2 % m
v2 = 0
odd_part = m
while odd_part % 2 == 0:
odd_part //= 2
v2 += 1
# Mod 2^v2
if v2 == 0:
mod_2part, p2 = 0, 1
else:
p2 = 1 << v2
small_towers = {0: 1, 1: 2, 2: 4, 3: 16, 4: 65536}
exp_val = small_towers.get(height - 1, 10**20)
mod_2part = 0 if exp_val >= v2 else (1 << exp_val) % p2
# Mod odd_part
if odd_part == 1:
mod_oddpart = 0
else:
lam = carmichael(odd_part)
exp_mod = tower2_mod(height - 1, lam)
mod_oddpart = pow(2, exp_mod, odd_part)
# CRT
if v2 == 0:
return mod_oddpart % m
inv_p2 = pow(p2, -1, odd_part)
diff = (mod_oddpart - mod_2part) % odd_part
t = diff * inv_p2 % odd_part
return (mod_2part + p2 * t) % m
M = 14 ** 8
a0, a1, a2, a3 = 1, 3, 7, 61
a4 = (tower2_mod(7, M) - 3) % M
stable = tower2_mod(200, M)
a5 = (stable - 3) % M
a6 = a5
print((a0 + a1 + a2 + a3 + a4 + a5 + a6) % M)