Project Euler Problem 123 Solution

Project Euler Problem 123 Solution

Prime Square Remainders

by {BetaProjects} | Project Euler & HackerRank
Difficulty: Easy

Project Euler Problem 123 Statement

Let $p_n$ be the $n$th prime: $2, 3, 5, 7, 11, \dots$, and let $r$ be the remainder when $(p_n - 1)^n + (p_n + 1)^n$ is divided by $p_n^2$.

For example, when $n = 3$, $p_3 = 5$, and $4^3 + 6^3 = 280 \equiv 5 \mod 25$.

The least value of $n$ for which the remainder first exceeds $10^9$ is $7037$.

Find the least value of $n$ for which the remainder first exceeds $10^{10}$.

Solution

Python Source Code

from sympy import primerange

for n, p in enumerate(primerange(250000), 1):
    if n % 2 == 1 and 2 * n * p > 10**10:
        print(n)
        break