Skip to content

Commit 2c9254c

Browse files
ksr1122Ravi Kandasamy Sundaram
authored andcommitted
Add solution for Project Euler problem 123 (TheAlgorithms#3072)
Name: Prime square remainders Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2. For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 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. Reference: https://projecteuler.net/problem=123 reference: TheAlgorithms#2695 Co-authored-by: Ravi Kandasamy Sundaram <[email protected]>
1 parent ebf9eea commit 2c9254c

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

Diff for: project_euler/problem_123/__init__.py

Whitespace-only changes.

Diff for: project_euler/problem_123/sol1.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
Problem 123: https://projecteuler.net/problem=123
3+
4+
Name: Prime square remainders
5+
6+
Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and
7+
let r be the remainder when (pn−1)^n + (pn+1)^n is divided by pn^2.
8+
9+
For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 5 mod 25.
10+
The least value of n for which the remainder first exceeds 10^9 is 7037.
11+
12+
Find the least value of n for which the remainder first exceeds 10^10.
13+
14+
15+
Solution:
16+
17+
n=1: (p-1) + (p+1) = 2p
18+
n=2: (p-1)^2 + (p+1)^2
19+
= p^2 + 1 - 2p + p^2 + 1 + 2p (Using (p+b)^2 = (p^2 + b^2 + 2pb),
20+
(p-b)^2 = (p^2 + b^2 - 2pb) and b = 1)
21+
= 2p^2 + 2
22+
n=3: (p-1)^3 + (p+1)^3 (Similarly using (p+b)^3 & (p-b)^3 formula and so on)
23+
= 2p^3 + 6p
24+
n=4: 2p^4 + 12p^2 + 2
25+
n=5: 2p^5 + 20p^3 + 10p
26+
27+
As you could see, when the expression is divided by p^2.
28+
Except for the last term, the rest will result in the remainder 0.
29+
30+
n=1: 2p
31+
n=2: 2
32+
n=3: 6p
33+
n=4: 2
34+
n=5: 10p
35+
36+
So it could be simplified as,
37+
r = 2pn when n is odd
38+
r = 2 when n is even.
39+
"""
40+
41+
from typing import Dict, Generator
42+
43+
44+
def sieve() -> Generator[int, None, None]:
45+
"""
46+
Returns a prime number generator using sieve method.
47+
>>> type(sieve())
48+
<class 'generator'>
49+
>>> primes = sieve()
50+
>>> next(primes)
51+
2
52+
>>> next(primes)
53+
3
54+
>>> next(primes)
55+
5
56+
>>> next(primes)
57+
7
58+
>>> next(primes)
59+
11
60+
>>> next(primes)
61+
13
62+
"""
63+
factor_map: Dict[int, int] = {}
64+
prime = 2
65+
while True:
66+
factor = factor_map.pop(prime, None)
67+
if factor:
68+
x = factor + prime
69+
while x in factor_map:
70+
x += factor
71+
factor_map[x] = factor
72+
else:
73+
factor_map[prime * prime] = prime
74+
yield prime
75+
prime += 1
76+
77+
78+
def solution(limit: float = 1e10) -> int:
79+
"""
80+
Returns the least value of n for which the remainder first exceeds 10^10.
81+
>>> solution(1e8)
82+
2371
83+
>>> solution(1e9)
84+
7037
85+
"""
86+
primes = sieve()
87+
88+
n = 1
89+
while True:
90+
prime = next(primes)
91+
if (2 * prime * n) > limit:
92+
return n
93+
# Ignore the next prime as the reminder will be 2.
94+
next(primes)
95+
n += 2
96+
97+
98+
if __name__ == "__main__":
99+
print(solution())

0 commit comments

Comments
 (0)