All Euler problems
Project Euler

Power Tower Modular Arithmetic

Define a power tower T(a, n) recursively: T(a, 1) = a and T(a, n) = a^(T(a, n-1)) for n >= 2. Find T(2, 100) mod (10^9 + 7).

Source sync Apr 19, 2026
Problem #0940
Level Level 20
Solved By 617
Languages C++, Python
Answer 969134784
Length 360 words
number_theorymodular_arithmeticrecursion

Problem Statement

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

The Fibonacci sequence \((f_i)\) is the unique sequence such that

  • \(f_0 = 0\)

  • \(f_1 = 1\)

  • \(f_{i + 1} = f_i + f{i - 1}\)

Similarly, there is a unique function \(A(m,n)\) such that

  • \(A(0, 0) = 0\)

  • \(A(0, 1) = 1\)

  • \(A(m + 1, n) = A(m, n + 1) + A(m, n)\)

  • \(A(m + 1, n + 1) = 2A(m + 1, n) + A(m, n)\)

Define \(S(k)=\displaystyle \sum _{i=2}^k\sum _{j=2}^k A(f_i,f_j)\). For example \begin {align} S(3)&=A(1,1)+A(1,2)+A(2,1)+A(2,2)\\ &=2+5+7+16\\ &=30 \end {align}

You are also given \(S(5)=10396\).

Find \(S(50)\), giving your answer modulo \(1123581313\).

Problem 940: Power Tower Modular Arithmetic

Mathematical Foundation

Theorem 1 (Euler’s theorem). If gcd(a,m)=1\gcd(a, m) = 1, then aφ(m)1(modm)a^{\varphi(m)} \equiv 1 \pmod{m}, where φ\varphi is Euler’s totient function.

Proof. The elements {ar:r(Z/mZ)}\{a \cdot r : r \in (\mathbb{Z}/m\mathbb{Z})^*\} form a permutation of (Z/mZ)(\mathbb{Z}/m\mathbb{Z})^*. Taking the product of all elements: aφ(m)rr(modm)a^{\varphi(m)} \prod r \equiv \prod r \pmod{m}. Since r\prod r is coprime to mm, we may cancel it. \square

Lemma 1 (Exponent reduction). If gcd(a,m)=1\gcd(a, m) = 1 and blog2mb \geq \log_2 m, then:

ababmodφ(m)(modm),a^b \equiv a^{b \bmod \varphi(m)} \pmod{m},

where the representative of bmodφ(m)b \bmod \varphi(m) is taken in {0,1,,φ(m)1}\{0, 1, \ldots, \varphi(m) - 1\}, except if bmodφ(m)=0b \bmod \varphi(m) = 0 and b>0b > 0, we use φ(m)\varphi(m) instead of 00.

Proof. Write b=qφ(m)+rb = q \cdot \varphi(m) + r with 0r<φ(m)0 \leq r < \varphi(m). Then ab=(aφ(m))qarar(modm)a^b = (a^{\varphi(m)})^q \cdot a^r \equiv a^r \pmod{m} by Theorem 1. The caveat about r=0r = 0 ensures correctness when b>0b > 0: we want ab≢a0=1a^b \not\equiv a^0 = 1 in general, but aφ(m)1a^{\varphi(m)} \equiv 1, which is consistent since r=0r = 0 means φ(m)b\varphi(m) \mid b. \square

Theorem 2 (Iterated totient reaches 1). For any m2m \geq 2, define φ(0)(m)=m\varphi^{(0)}(m) = m and φ(k+1)(m)=φ(φ(k)(m))\varphi^{(k+1)}(m) = \varphi(\varphi^{(k)}(m)). Then φ(k)(m)=1\varphi^{(k)}(m) = 1 for all klog2m+1k \geq \lfloor \log_2 m \rfloor + 1.

Proof. For m2m \geq 2: φ(m)m/2\varphi(m) \leq m/2 (since at least half the integers in {1,,m}\{1, \ldots, m\} are even, and if mm is even, φ(m)m/2\varphi(m) \leq m/2; if mm is odd, φ(m)m1<m\varphi(m) \leq m - 1 < m, and one can show φ(m)m/2\varphi(m) \leq m/2 for m3m \geq 3 by considering the factor of the smallest prime dividing mm). For m=2m = 2: φ(2)=1\varphi(2) = 1.

By induction: φ(k)(m)m/2k\varphi^{(k)}(m) \leq m / 2^k. When m/2k<2m / 2^k < 2, i.e., k>log2m1k > \log_2 m - 1, we have φ(k)(m)=1\varphi^{(k)}(m) = 1. Thus k=log2m+1k = \lfloor \log_2 m \rfloor + 1 suffices. \square

Theorem 3 (Tower convergence modulo mm). For a2a \geq 2 and m2m \geq 2, T(a,n)modmT(a, n) \bmod m stabilizes for nlog2m+1n \geq \lfloor \log_2 m \rfloor + 1. In particular, T(2,100)mod(109+7)=T(2,31)mod(109+7)T(2, 100) \bmod (10^9 + 7) = T(2, 31) \bmod (10^9 + 7).

Proof. By Theorem 2, the iterated totient chain m,φ(m),φ(2)(m),m, \varphi(m), \varphi^{(2)}(m), \ldots reaches 1 in at most log2m+131\lfloor \log_2 m \rfloor + 1 \leq 31 steps (for m=109+7m = 10^9 + 7). Once the modulus is 1, all values are 0 mod 1, so deeper levels of the tower do not affect the computation. Since 100>31100 > 31, the tower has converged. \square

Lemma 2 (Primality of 109+710^9 + 7). The number p=109+7p = 10^9 + 7 is prime, so φ(p)=p1=109+6\varphi(p) = p - 1 = 10^9 + 6.

Proof. Verified by standard primality testing (e.g., Miller—Rabin with sufficient witnesses, or the AKS algorithm). \square

Editorial

Compute T(2, 100) mod (10^9 + 7), where T(a, n) = a^a^a^…^a (n copies of a) evaluated right-to-left (power tower / tetration). Key technique: By Euler’s theorem, a^x mod m = a^(x mod phi(m)) mod m (when gcd(a,m)=1). We recursively reduce the exponent modulo the iterated totient chain: m -> phi(m) -> phi(phi(m)) -> … -> 1 The chain from 10^9+7 has finite length, so the tower stabilizes. Results:. We base cases. We then by Lemma 1, we need T(a, n-1) mod phi(m). Finally, handle the case where exponent == 0 but actual value > 0.

Pseudocode

Base cases
Recursive case: T(a, n) = a^{T(a, n-1)} mod m
By Lemma 1, we need T(a, n-1) mod phi(m)
Handle the case where exponent == 0 but actual value > 0
Since a = 2 and n >= 2, T(2, n-1) >= 2, so exponent should
be interpreted correctly
Main call

Complexity Analysis

  • Time: The recursion depth is at most d=log2m+130d = \lfloor \log_2 m \rfloor + 1 \approx 30. At each level, we compute φ(mi)\varphi(m_i) in O(mi)O(\sqrt{m_i}) and perform modular exponentiation in O(logmi)O(\log m_i). The dominant cost is the totient computation at the top level: O(m)O(\sqrt{m}). Total: O(dm)=O(mlogm)O(d \cdot \sqrt{m}) = O(\sqrt{m} \log m).
  • Space: O(d)=O(logm)O(d) = O(\log m) for the recursion stack.

Answer

969134784\boxed{969134784}

Code

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

C++ project_euler/problem_940/solution.cpp
#include <bits/stdc++.h>
using namespace std;
long long euler_phi(long long n){
    long long r=n,t=n;
    for(long long p=2;p*p<=t;p++){
        if(t%p==0){while(t%p==0)t/=p;r-=r/p;}
    }
    if(t>1) r-=r/t;
    return r;
}
long long power(long long a,long long b,long long m){
    long long r=1; a%=m;
    while(b>0){if(b&1) r=r*a%m; a=a*a%m; b>>=1;}
    return r;
}
long long tower(long long a,int n,long long m){
    if(m==1) return 0;
    if(n==1) return a%m;
    long long phi=euler_phi(m);
    long long exp=tower(a,n-1,phi);
    return power(a,exp,m);
}
int main(){
    cout<<tower(2,100,1000000007LL)<<endl;
}