All Euler problems
Project Euler

Divisor Chain Lengths

A divisor chain starting from n is a sequence n = a_1 > a_2 >... > a_k = 1 where each a_(i+1) | a_i. The length of such a chain is k - 1. Let L(n) denote the maximum chain length. Compute sum_(n=1)...

Source sync Apr 19, 2026
Problem #0921
Level Level 34
Solved By 226
Languages C++, Python
Answer 378401935
Length 228 words
number_theorymodular_arithmeticanalytic_math

Problem Statement

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

Consider the following recurrence relation: \begin {align} a_0 &= \frac {\sqrt 5 + 1}2\\ a_{n+1} &= \dfrac {a_n(a_n^4 + 10a_n^2 + 5)}{5a_n^4 + 10a_n^2 + 1} \end {align}

Note that \(a_0\) is the golden ratio.

\(a_n\) can always be written in the form \(\dfrac {p_n\sqrt {5}+1}{q_n}\), where \(p_n\) and \(q_n\) are positive integers.

Let \(s(n)=p_n^5+q_n^5\). So, \(s(0)=1^5+2^5=33\).

The Fibonacci sequence is defined as: \(F_1=1\), \(F_2=1\), \(F_n=F_{n-1}+F_{n-2}\) for \(n > 2\).

Define \(\displaystyle S(m)=\sum _{i=2}^{m}s(F_i)\).

Find \(S(1618034)\). Submit your answer modulo \(398874989\).

Problem 921: Divisor Chain Lengths

Mathematical Foundation

Theorem 1 (Maximum Divisor Chain Length). For every positive integer nn, L(n)=Ω(n)L(n) = \Omega(n), where Ω(n)=pknk\Omega(n) = \sum_{p^k \| n} k denotes the number of prime factors of nn counted with multiplicity.

Proof. We establish matching upper and lower bounds.

Upper bound. Let n=a1>a2>>ak=1n = a_1 > a_2 > \cdots > a_k = 1 be any divisor chain. At each step, ai+1aia_{i+1} \mid a_i and ai+1<aia_{i+1} < a_i, so ai/ai+12a_i / a_{i+1} \geq 2. Hence the quotient ai/ai+1a_i / a_{i+1} contributes at least one prime factor (with multiplicity), giving Ω(ai)Ω(ai+1)+1\Omega(a_i) \geq \Omega(a_{i+1}) + 1. Summing telescopically:

k1Ω(a1)Ω(ak)=Ω(n)0=Ω(n).k - 1 \leq \Omega(a_1) - \Omega(a_k) = \Omega(n) - 0 = \Omega(n).

Lower bound. Write n=p1e1p2e2prern = p_1^{e_1} p_2^{e_2} \cdots p_r^{e_r}. Construct the chain by removing one prime factor at a time:

nn/p1n/p12n/p1e1n/(p1e1p2)1.n \to n/p_1 \to n/p_1^2 \to \cdots \to n/p_1^{e_1} \to n/(p_1^{e_1} p_2) \to \cdots \to 1.

This chain has exactly e1+e2++er=Ω(n)e_1 + e_2 + \cdots + e_r = \Omega(n) steps. \square

Theorem 2 (Summation Formula). For all N1N \geq 1:

n=1NΩ(n)=pNp primek=1logpNNpk.\sum_{n=1}^{N} \Omega(n) = \sum_{\substack{p \leq N \\ p \text{ prime}}} \sum_{k=1}^{\lfloor \log_p N \rfloor} \left\lfloor \frac{N}{p^k} \right\rfloor.

Proof. By definition, Ω(n)=pknp prime,k11\Omega(n) = \sum_{\substack{p^k \mid n \\ p \text{ prime},\, k \geq 1}} 1. Exchanging the order of summation:

n=1NΩ(n)=n=1Npkn1=p primek1pkN{nN:pkn}=p,kNpk.\sum_{n=1}^{N} \Omega(n) = \sum_{n=1}^{N} \sum_{\substack{p^k \mid n}} 1 = \sum_{\substack{p \text{ prime} \\ k \geq 1 \\ p^k \leq N}} \left|\left\{n \leq N : p^k \mid n\right\}\right| = \sum_{p,k} \left\lfloor \frac{N}{p^k} \right\rfloor. \quad \square

Lemma 1 (Asymptotic Behavior). n=1NΩ(n)=NlnlnN+M1N+O ⁣(NlnN)\displaystyle\sum_{n=1}^{N} \Omega(n) = N \ln \ln N + M_1 N + O\!\left(\frac{N}{\ln N}\right), where M1=γ+p(lnpp11p)M_1 = \gamma + \sum_{p}\left(\ln\frac{p}{p-1} - \frac{1}{p}\right) and γ\gamma is the Euler—Mascheroni constant.

Proof. From Theorem 2:

n=1NΩ(n)=pNNp+O ⁣(pk2Npk)=NpN1p+O(N).\sum_{n=1}^{N} \Omega(n) = \sum_{p \leq N} \frac{N}{p} + O\!\left(\sum_{p} \sum_{k \geq 2} \frac{N}{p^k}\right) = N \sum_{p \leq N} \frac{1}{p} + O(N).

By Mertens’ second theorem, pN1p=lnlnN+M+O(1/lnN)\sum_{p \leq N} \frac{1}{p} = \ln\ln N + M + O(1/\ln N) where MM is the Meissel—Mertens constant. The result follows. \square

Editorial

Alternative direct formula (no sieve needed). We sieve approach: compute Omega(n) for all n <= N. We then p is prime; mark composites. Finally, iterate over each prime power p^k, add 1 to all multiples.

Pseudocode

Sieve approach: compute Omega(n) for all n <= N
p is prime; mark composites
For each prime power p^k, add 1 to all multiples
Use Theorem 2 directly

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for both the sieve-based and direct approaches. The sieve of Eratosthenes runs in O(NloglogN)O(N \log \log N). The inner loop over prime powers contributes pkN/pk=O(NloglogN)\sum_{p} \sum_{k} N/p^k = O(N \log \log N).
  • Space: O(N)O(N) for the sieve array. The direct formula method requires O(N/lnN)O(N / \ln N) space for the prime list, or O(N)O(N) for a bit sieve.

Answer

378401935\boxed{378401935}

Code

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

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

/*
 * Problem 921: Divisor Chain Lengths
 *
 * L(n) = max divisor chain length from n to 1 = Omega(n).
 * Find sum_{n=1}^{10^5} Omega(n) mod 10^9+7.
 *
 * Key insight: The longest chain from n to 1 via proper divisors
 * has length Omega(n) = sum of exponents in prime factorization.
 *
 * Proof:
 *   Upper bound: each step a_{i+1} | a_i, a_{i+1} < a_i implies
 *     a_i/a_{i+1} >= 2, so Omega decreases by >= 1 per step.
 *   Lower bound: divide by one prime at a time.
 *
 * Sieve approach: for each prime p, for each power p^k <= N,
 * add 1 to Omega(m) for all multiples m of p^k.
 *
 * Alternative formula: sum Omega(n) = sum_{p prime} sum_k floor(N/p^k).
 *
 * Complexity: O(N log log N) sieve, O(N) summation.
 */

int main() {
    const int N = 100000;
    const long long MOD = 1000000007;

    vector<int> omega(N + 1, 0);
    for (int p = 2; p <= N; p++) {
        if (omega[p] == 0) { // p is prime
            for (long long pk = p; pk <= N; pk *= p) {
                for (long long m = pk; m <= N; m += pk) {
                    omega[m]++;
                }
            }
        }
    }

    long long ans = 0;
    for (int i = 1; i <= N; i++) {
        ans += omega[i];
    }

    cout << ans % MOD << endl;

    // Verification
    assert(omega[1] == 0);
    assert(omega[6] == 2);   // 2 * 3
    assert(omega[8] == 3);   // 2^3
    assert(omega[12] == 3);  // 2^2 * 3
    assert(omega[60] == 4);  // 2^2 * 3 * 5
    assert(omega[64] == 6);  // 2^6

    return 0;
}