Skip to content

Commit c585cb1

Browse files
mindauglMaximSmolskiypre-commit-ci[bot]
authored
Solution for the Euler Project problem 136 (#12658)
* Add initial version of file for the Euler project problem 136 solution. * Add documentation and tests for the Euler project problem 136 solution. * Update sol1.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update sol1.py * Update sol1.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update sol1.py * Update sol1.py * Update sol1.py * Update sol1.py * Update sol1.py * Update sol1.py --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4282063 commit c585cb1

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

project_euler/problem_136/__init__.py

Whitespace-only changes.

project_euler/problem_136/sol1.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Project Euler Problem 136: https://projecteuler.net/problem=136
3+
4+
Singleton Difference
5+
6+
The positive integers, x, y, and z, are consecutive terms of an arithmetic progression.
7+
Given that n is a positive integer, the equation, x^2 - y^2 - z^2 = n,
8+
has exactly one solution when n = 20:
9+
13^2 - 10^2 - 7^2 = 20.
10+
11+
In fact there are twenty-five values of n below one hundred for which
12+
the equation has a unique solution.
13+
14+
How many values of n less than fifty million have exactly one solution?
15+
16+
By change of variables
17+
18+
x = y + delta
19+
z = y - delta
20+
21+
The expression can be rewritten:
22+
23+
x^2 - y^2 - z^2 = y * (4 * delta - y) = n
24+
25+
The algorithm loops over delta and y, which is restricted in upper and lower limits,
26+
to count how many solutions each n has.
27+
In the end it is counted how many n's have one solution.
28+
"""
29+
30+
31+
def solution(n_limit: int = 50 * 10**6) -> int:
32+
"""
33+
Define n count list and loop over delta, y to get the counts, then check
34+
which n has count == 1.
35+
36+
>>> solution(3)
37+
0
38+
>>> solution(10)
39+
3
40+
>>> solution(100)
41+
25
42+
>>> solution(110)
43+
27
44+
"""
45+
n_sol = [0] * n_limit
46+
47+
for delta in range(1, (n_limit + 1) // 4 + 1):
48+
for y in range(4 * delta - 1, delta, -1):
49+
n = y * (4 * delta - y)
50+
if n >= n_limit:
51+
break
52+
n_sol[n] += 1
53+
54+
ans = 0
55+
for i in range(n_limit):
56+
if n_sol[i] == 1:
57+
ans += 1
58+
59+
return ans
60+
61+
62+
if __name__ == "__main__":
63+
print(f"{solution() = }")

0 commit comments

Comments
 (0)