All Euler problems
Project Euler

Tribonacci Non-divisors

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

Source sync May 21, 2026
Problem #0225
Level Level 07
Solved By 5,031
Languages C++, Python
Answer 2009
Length 265 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 Development

Modulo any positive integer mm, the sequence is controlled by triples

(Tn,Tn+1,Tn+2)(modm).(T_n,T_{n+1},T_{n+2}) \pmod m.

There are only m3m^3 such triples, so some triple must repeat. The recurrence is invertible:

Tn−1=Tn+2−Tn+1−Tn.T_{n-1}=T_{n+2}-T_{n+1}-T_n.

Therefore once a triple repeats, the whole sequence before and after that point repeats as well. In particular, the Tribonacci sequence modulo mm is purely periodic.

So to decide whether mm divides any Tribonacci number, it is enough to start from

(1,1,1)(1,1,1)

and follow the sequence modulo mm until one of two things happens:

  • a term becomes 00, so mm is a divisor of some Tribonacci number;
  • the triple returns to (1,1,1)(1,1,1), so one full period has been traversed without seeing 00.

Because every Tribonacci number is odd, only odd candidates can possibly fail to divide the sequence.

Editorial

The key observation is that this is not a growth problem at all; it is a finite-state problem. Modulo mm, the sequence never produces anything except triples of residues, and there are only m3m^3 of those. Once the initial triple comes back, the cycle is closed and nothing new can happen.

That means every candidate odd number can be tested independently with constant memory. The program walks the Tribonacci recurrence modulo mm, stops immediately if it ever hits 00, and otherwise declares success when the state returns to (1,1,1)(1,1,1). Counting such odd moduli in increasing order gives the required 124124th example.

Pseudocode

Set the target count to 124.
Initialize the current odd candidate m = 1.

Repeat forever:
    Skip m = 1 because it divides every integer.

    Start the Tribonacci state modulo m at (1, 1, 1).

    Advance the recurrence:
        next = (a + b + c) mod m
        shift (a, b, c) to (b, c, next)

        If c becomes 0:
            m divides some Tribonacci number
            stop testing this m

        If the state returns to (1, 1, 1):
            m is a Tribonacci non-divisor
            increase the running count
            if this is the 124th one, print m and stop

    Move on to the next odd number.

Complexity Analysis

  • Time: For a fixed modulus mm, at most one full period is explored. The crude bound is O(m3)O(m^3), though the actual periods are much shorter.
  • Space: O(1)O(1).

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;
}