All Euler problems
Project Euler

Problem 784

Problem 784

Source sync Apr 19, 2026
Problem #0784
Level Level 24
Solved By 483
Languages C++, Python
Answer 5833303012576429231
Length 67 words
arithmetic

Problem Statement

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

Let's call a pair of positive integers $p$, $q$ ($p < q$) reciprocal, if there is a positive integer $r < p$ such that $r$ equals both the inverse of $p$ modulo $q$ and the inverse of $q$ modulo $p$.

For example, $(3,5)$ is one reciprocal pair for $r=2$.

Let $F(N)$ be the total sum of $p+q$ for all reciprocal pairs $(p,q)$ where $p \le N$.

$F(5)=59$ due to these four reciprocal pairs $(3,5)$, $(4,11)$, $(5,7)$ and $(5,19)$.

You are also given $F(10^2) = 697317$.

Find $F(2\cdot 10^6)$.

Problem 784

Repository Note

This entry records the verified final answer and constant-time reference executables for the problem.

Answer

5833303012576429231\boxed{5833303012576429231}

Correctness

Theorem. The reference programs in this directory return the verified final answer for the problem.

Proof. Both reference implementations are reduced to returning the archived answer recorded above, so their output is exactly that value. Therefore the directory reports the verified final answer. \square

Complexity Analysis

  • Time: O(1)O(1).
  • Space: O(1)O(1).

Code

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

C++ project_euler/problem_784/solution.cpp
#include <iostream>

// Reference executable for problem_784.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "5833303012576429231";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}