All Euler problems
Project Euler

Tribonacci Non-divisors

The Tribonacci sequence T_n is defined by T_1 = T_2 = T_3 = 1 and T_n = T_(n-1) + T_(n-2) + T_(n-3) for n > 3. Find the 124th odd number that does not divide any Tribonacci number.

Source sync Apr 19, 2026
Problem #0225
Level Level 07
Solved By 5,031
Languages C++, Python
Answer 2009
Length 361 words
sequencemodular_arithmeticnumber_theory

Problem Statement

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

The sequence \(1, 1, 1, 3, 5, 9, 17, 31, 57, 105, 193, 355, 653, 1201, \dots \)

is defined by \(T_1 = T_2 = T_3 = 1\) and \(T_n = T_{n - 1} + T_{n - 2} + T_{n - 3}\).

It can be shown that \(27\) does not divide any terms of this sequence.

In fact, \(27\) is the first odd number with this property.

Find the \(124^{th}\) odd number that does not divide any terms of the above sequence.

Problem 225: Tribonacci Non-divisors

Mathematical Foundation

Theorem 1 (All Tribonacci Numbers Are Odd). For all n1n \geq 1, TnT_n is odd.

Proof. Base cases: T1=T2=T3=1T_1 = T_2 = T_3 = 1 are odd. Inductive step: assume Tn3,Tn2,Tn1T_{n-3}, T_{n-2}, T_{n-1} are all odd. Then Tn=Tn1+Tn2+Tn31+1+11(mod2)T_n = T_{n-1} + T_{n-2} + T_{n-3} \equiv 1 + 1 + 1 \equiv 1 \pmod{2}, which is odd. By strong induction, all TnT_n are odd. \square

Theorem 2 (Pure Periodicity Modulo mm). For any positive integer mm, the Tribonacci sequence modulo mm is purely periodic.

Proof. Consider the sequence of triples (Tnmodm,Tn+1modm,Tn+2modm)(T_n \bmod m, T_{n+1} \bmod m, T_{n+2} \bmod m). There are m3m^3 possible triples, so by the pigeonhole principle, some triple must repeat: there exist i<ji < j with

(Ti,Ti+1,Ti+2)(Tj,Tj+1,Tj+2)(modm).(T_i, T_{i+1}, T_{i+2}) \equiv (T_j, T_{j+1}, T_{j+2}) \pmod{m}.

The recurrence is invertible: Tn1=Tn+2Tn+1TnT_{n-1} = T_{n+2} - T_{n+1} - T_n. Therefore we can extend the sequence backwards uniquely. Applying the inverse recurrence jij - i times from the repeated triple shows that (T1,T2,T3)(Tji+1,Tji+2,Tji+3)(modm)(T_1, T_2, T_3) \equiv (T_{j-i+1}, T_{j-i+2}, T_{j-i+3}) \pmod{m}. Hence the sequence is purely periodic with period dividing jij - i. \square

Lemma 1 (Period Bound). The period of the Tribonacci sequence modulo mm is at most m3m^3.

Proof. There are m3m^3 possible triples of residues, so a repetition must occur within m3+1m^3 + 1 steps. \square

Theorem 3 (Divisibility Criterion). An odd number mm divides some Tribonacci number TnT_n if and only if 00 appears in one complete period of the sequence (Tnmodm)(T_n \bmod m).

Proof. (\Rightarrow) If mTnm \mid T_n, then Tnmodm=0T_n \bmod m = 0 appears in the sequence. (\Leftarrow) If 00 appears in one period, then since the sequence is purely periodic, Tk0(modm)T_k \equiv 0 \pmod{m} for some kk. Conversely, if 00 never appears in one full period, the pure periodicity guarantees it never appears at all. \square

Lemma 2 (Cycle Detection). To test whether 00 appears in the period, compute (Tn,Tn+1,Tn+2)modm(T_n, T_{n+1}, T_{n+2}) \bmod m starting from (1,1,1)(1, 1, 1) until either Tn0T_n \equiv 0 (divisor found) or the triple returns to (1,1,1)(1, 1, 1) (non-divisor confirmed).

Proof. By Theorem 2, the sequence is purely periodic starting from (1,1,1)(1,1,1). If (1,1,1)(1,1,1) recurs, one full period has been traversed. If 00 was not encountered, it never will be. \square

Editorial

T(1) = T(2) = T(3) = 1, T(n) = T(n-1) + T(n-2) + T(n-3). For each odd m, compute T(n) mod m. If 0 never appears in the cycle, then m is a non-divisor. We loop.

Pseudocode

while true
loop

Complexity Analysis

  • Time: For each candidate mm, the cycle detection runs for at most m3m^3 iterations, but in practice the period is much shorter (typically O(m)O(m)). We test odd numbers up to approximately 2009. Total work is bounded by O ⁣(m oddπ(m))O\!\left(\sum_{m \text{ odd}} \pi(m)\right) where π(m)\pi(m) is the actual period, empirically O(mmax2)O(m_{\max}^2).
  • Space: O(1)O(1) per candidate (only three residues stored).

Answer

2009\boxed{2009}

Code

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

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

int main(){
    // Find the 124th odd number that does not divide any Tribonacci number.
    // T(1) = T(2) = T(3) = 1, T(n) = T(n-1) + T(n-2) + T(n-3).
    // For each odd m, compute T(n) mod m until 0 is found or cycle detected.

    int target = 124;
    int count = 0;

    for(int m = 1; ; m += 2){
        // Check if m divides any Tribonacci number
        int a = 1 % m, b = 1 % m, c = 1 % m;
        bool divides = false;

        // Check if m = 1 (divides everything)
        if(m == 1){
            // T(1) = 1, and 1 divides 1. So 1 divides a tribonacci number.
            continue;
        }

        // Iterate through the cycle
        for(long long iter = 0; iter < (long long)m * m * m + 10; iter++){
            int next = (a + b + c) % m;
            a = b;
            b = c;
            c = next;
            if(c == 0){
                divides = true;
                break;
            }
            // Check if we've returned to (1, 1, 1)
            if(a == 1 % m && b == 1 % m && c == 1 % m){
                break;
            }
        }

        if(!divides){
            count++;
            if(count == target){
                cout << m << endl;
                return 0;
            }
        }
    }

    return 0;
}