All Euler problems
Project Euler

Additive Prime Counting

An additive prime is a prime whose digit sum is also prime. Find the count of additive primes below 10^6.

Source sync Apr 19, 2026
Problem #0969
Level Level 33
Solved By 248
Languages C++, Python
Answer 412543690
Length 215 words
number_theorymodular_arithmeticprobability

Problem Statement

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

Starting at zero, a kangaroo hops along the real number line in the positive direction. Each successive hop takes the kangaroo forward a uniformly random distance between and . Let be the expected number of hops needed for the kangaroo to pass on the real line.

If we write , then for all positive integers , can be expressed as a polynomial function of with rational coefficients. For example . Define to be the sum of all integer coefficients in this polynomial form of . Therefore and .
You are also given .
Find . Give your answer modulo .

Problem 969: Additive Prime Counting

Mathematical Analysis

Digit Sum Properties

Definition. The digit sum d(n)=idid(n) = \sum_{i} d_i where did_i are the base-10 digits of nn.

Theorem. For n<106n < 10^6, the digit sum satisfies 1d(n)541 \le d(n) \le 54 (max is 999999999999 with digit sum 5454).

Proposition. nd(n)(mod9)n \equiv d(n) \pmod{9}, so for prime p>3p > 3, d(p)≢0(mod3)d(p) \not\equiv 0 \pmod{3} (otherwise 3p3 \mid p, contradiction). Thus d(p)d(p) avoids multiples of 3 except when p=3p = 3.

Density Estimate

Among primes below NN, the digit sum d(p)d(p) is roughly normally distributed with mean 4.5log10N\approx 4.5 \cdot \log_{10} N and standard deviation 4.5log10N0.83\approx \sqrt{4.5 \cdot \log_{10} N \cdot 0.83}. The probability that a random integer in the digit-sum range is prime is approximately 1/ln(digit sum)1/\ln(\text{digit sum}).

For N=106N = 10^6: average digit sum 27\approx 27, and primes up to 54 include 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 (16 primes out of 54). Roughly 30% of digit sums are prime, so we expect 0.3×7849823500\approx 0.3 \times 78498 \approx 23500 additive primes. The actual count is slightly higher.

Derivation

Editorial

These are called “additive primes.”. We sieve primes below 10610^6. We then precompute the set of primes up to 54. Finally, iterate over each prime pp, compute digit sum and check if it is prime.

Pseudocode

Sieve primes below $10^6$
Precompute the set of primes up to 54
For each prime $p$, compute digit sum and check if it is prime
Count matches

Proof of Correctness

The sieve is exact. Digit sum computation by repeated modular division is exact. Primality check for numbers up to 54 is trivial.

Complexity Analysis

O(NloglogN)O(N \log \log N) for sieve, O(π(N)log10N)O(\pi(N) \cdot \log_{10} N) for digit sum checks.

Answer

412543690\boxed{412543690}

Code

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

C++ project_euler/problem_969/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    const int N = 1000000;
    vector<bool> is_p(N, true); is_p[0]=is_p[1]=false;
    for (int i=2;(long long)i*i<N;i++) if(is_p[i]) for(int j=i*i;j<N;j+=i) is_p[j]=false;
    set<int> small_p;
    for (int i=2;i<55;i++) if(is_p[i]) small_p.insert(i);
    int cnt=0;
    for(int p=2;p<N;p++){
        if(!is_p[p]) continue;
        int ds=0,t=p; while(t){ds+=t%10;t/=10;}
        if(small_p.count(ds)) cnt++;
    }
    cout<<cnt<<endl;
    return 0;
}