All Euler problems
Project Euler

Power Sequence

Tetration (iterated exponentiation) is defined recursively: ^0 a = 1 and ^(n+1) a = a^^n a. Compute ^n a mod m for given a, n, m.

Source sync Apr 19, 2026
Problem #0855
Level Level 37
Solved By 169
Languages C++, Python
Answer 6.8827571976e-57
Length 358 words
number_theorymodular_arithmeticsequence

Problem Statement

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

Given two positive integers \(a,b\), Alex and Bianca play a game in \(ab\) rounds. They begin with a square piece of paper of side length \(1\).

In each round Alex divides the current rectangular piece of paper into \(a \times b\) pieces using \(a-1\) horizontal cuts and \(b-1\) vertical ones. The cuts do not need to be evenly spaced. Moreover, a piece can have zero width/height when a cut coincides with another cut or the edge of the paper. The pieces are then numbered \(1, 2, ..., ab\) starting from the left top corner, moving from left to right and starting from the left of the next row when a row is finished.

Then Bianca chooses one of the pieces for the game to continue on. However, Bianca must not choose a piece with a number she has already chosen during the game.

Bianca wants to minimize the area of the final piece of paper while Alex wants to maximize it. Let \(S(a,b)\) be the area of the final piece assuming optimal play.

For example, \(S(2,2) = 1/36\) and \(S(2, 3) = 1/1800 \approx 5.5555555556\mathrm {e}{-4}\).

Find \(S(5,8)\). Give your answer in scientific notation rounded to ten significant digits after the decimal point. Use a lowercase e to separate the mantissa and the exponent.

Problem 855: Power Sequence

Mathematical Analysis

Euler’s Theorem for Modular Tetration

Theorem (Euler). If gcd(a,m)=1\gcd(a, m) = 1, then aφ(m)1(modm)a^{\varphi(m)} \equiv 1 \pmod{m}.

Corollary. ababmodφ(m)(modm)a^b \equiv a^{b \bmod \varphi(m)} \pmod{m} when gcd(a,m)=1\gcd(a, m) = 1 and blog2mb \ge \log_2 m.

Recursive Reduction

To compute namodm{}^n a \bmod m:

  1. If m=1m = 1, return 0.
  2. Compute e=n1amodφ(m)e = {}^{n-1} a \bmod \varphi(m) recursively.
  3. Return aemodma^e \bmod m.

The recursion terminates because φ(φ((m)))=1\varphi(\varphi(\cdots(m))) = 1 after O(logm)O(\log m) steps.

Theorem. The tower φ(k)(m)=1\varphi^{(k)}(m) = 1 after at most O(logm)O(\log m) iterations, since φ(m)m/2\varphi(m) \le m/2 for m>1m > 1.

Lifting the Exponent Lemma

When gcd(a,m)>1\gcd(a, m) > 1, we need more care. If ak0(modm)a^k \equiv 0 \pmod{m} for some small kk, and the tower height exceeds kk, the answer is 0.

Concrete Examples

aannmmnamodm{}^n a \bmod m
21102
22104
23106 (24=162^4 = 16)
24106 (216=655362^{16} = 65536)
3276 (33=2763^3 = 27 \equiv 6)
2310036 (216=655362^{16} = 65536)

Verification for 32mod10{}^3 2 \bmod 10: 32=222=24=166(mod10){}^3 2 = 2^{2^2} = 2^4 = 16 \equiv 6 \pmod{10}. Correct.

Verification for 42mod10{}^4 2 \bmod 10: 42=232=216=655366(mod10){}^4 2 = 2^{{}^3 2} = 2^{16} = 65536 \equiv 6 \pmod{10}.

Convergence of Tetration Mod mm

Theorem. For fixed aa and mm with gcd(a,m)=1\gcd(a, m) = 1, the sequence namodm{}^n a \bmod m eventually stabilizes (becomes constant) for nlog2mn \ge \lceil\log_2 m\rceil.

Complexity Analysis

  • Recursive φ\varphi-reduction: O(log2m)O(\log^2 m) for the recursion depth times modular exponentiation.
  • Computing φ\varphi: O(m)O(\sqrt{m}) per level via trial division.
  • Total: O(logmm)O(\log m \cdot \sqrt{m}).

Knuth’s Up-Arrow Notation

Tetration is an=naa \uparrow\uparrow n = {}^n a. In Knuth’s notation: ab=aba \uparrow b = a^b, ab=aaaa \uparrow\uparrow b = a^{a^{\cdots^a}} (bb copies). This extends to higher hyperoperations: aba \uparrow\uparrow\uparrow b (pentation), etc.

Why the Algorithm Terminates

Theorem. The φ\varphi-reduction chain m,φ(m),φ(φ(m)),m, \varphi(m), \varphi(\varphi(m)), \ldots reaches 1 after at most 2log2m2\log_2 m steps.

Proof. For even mm: φ(m)m/2\varphi(m) \le m/2. For odd m>1m > 1: φ(m)\varphi(m) is even (since φ(n)\varphi(n) is even for n>2n > 2), so the next step halves. Every two steps, the value at least halves. \square

Handling gcd(a,m)>1\gcd(a, m) > 1

When gcd(a,m)>1\gcd(a, m) > 1, Euler’s theorem doesn’t directly apply. The correct formula is:

abmodm={abmodmif b<log2m (compute directly)a(bmodφ(m))+φ(m)modmif blog2ma^b \bmod m = \begin{cases} a^b \bmod m & \text{if } b < \log_2 m \text{ (compute directly)} \\ a^{(b \bmod \varphi(m)) + \varphi(m)} \bmod m & \text{if } b \ge \log_2 m \end{cases}

The "+φ(m)+\varphi(m)" ensures we’re in the periodic regime of akmodma^k \bmod m.

Infinite Tetration

Theorem. The infinite tower a=limnna{}^\infty a = \lim_{n\to\infty} {}^n a converges iff eeae1/ee^{-e} \le a \le e^{1/e}, i.e., approximately 0.0660a1.44470.0660 \le a \le 1.4447.

For a=2a = \sqrt{2}: 2=2{}^\infty \sqrt{2} = 2 since (2)2=2(\sqrt{2})^2 = 2.

For a=e1/e1.4447a = e^{1/e} \approx 1.4447: a=e{}^\infty a = e.

Connection to Ackermann Function

Tetration is equivalent to the Ackermann function at level 3: A(3,n)=2n+33A(3, n) = 2^{n+3} - 3 grows as tetration of 2.

Fixed Points of Modular Tetration

Theorem. For fixed aa and mm, the sequence namodm{}^n a \bmod m is eventually constant. The stabilization index is at most depth(φ,m)\text{depth}(\varphi, m) where depth(φ,m)=min{k:φ(k)(m)=1}\text{depth}(\varphi, m) = \min\{k : \varphi^{(k)}(m) = 1\}.

aammdepth\text{depth}Stable value
21046
3736
2100536
51345

Answer

6.8827571976e57\boxed{6.8827571976e-57}

Code

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

C++ project_euler/problem_855/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

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

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

ll tetration_mod(ll a, ll n, ll m) {
    if (m == 1) return 0;
    if (n == 0) return 1 % m;
    if (n == 1) return a % m;
    ll phi = euler_totient(m);
    ll exp = tetration_mod(a, n-1, phi);
    if (__gcd(a, m) == 1) return power(a, exp, m);
    return power(a, exp + phi, m);
}

int main() {
    assert(tetration_mod(2, 3, 10) == 6);
    assert(tetration_mod(2, 4, 10) == 6);
    cout << tetration_mod(2, 100, 1000000007) << endl;
    return 0;
}