Skip to content

Commit 4ecfa75

Browse files
committed
Add solution for the euler project problem 138.
1 parent 0c8cf8e commit 4ecfa75

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

project_euler/problem_138/__init__.py

Whitespace-only changes.

project_euler/problem_138/sol1.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
Project Euler Problem 138: https://projecteuler.net/problem=138
3+
4+
Special Isosceles Triangles
5+
6+
7+
With change of variables
8+
9+
c = b/2
10+
11+
and requiring that
12+
13+
h = 2c +- 1
14+
15+
the triangle relation
16+
17+
c^2 + h^2 = L^2
18+
19+
can be expressed as
20+
21+
5 c^2 +- 4c + 1 = L^2
22+
23+
or with some rearrangement:
24+
25+
(5c +- 2)^2 = 5L^2 - 1
26+
27+
This to be solved for positive integer c and L, requires that
28+
29+
5L^2 - 1 = m^2
30+
31+
The above equation is negative Pell's equation with n = 5 and can be solved
32+
recursively as outlined in the wikipedia article.
33+
Note, we neglect first solution (m = 2, L = 1), as this leads to b and h
34+
being non-integers.
35+
36+
Reference: https://en.wikipedia.org/wiki/Pell%27s_equation#The_negative_Pell's_equation
37+
38+
"""
39+
40+
41+
def solution(k: int = 12) -> int:
42+
"""
43+
The recursive solution of negative Pell's equation with k + 1 values of L
44+
summed and the first solution being skipped.
45+
46+
>>> solution(2)
47+
322
48+
>>> solution(5)
49+
1866293
50+
"""
51+
52+
m_i = 2
53+
l_i = 1
54+
ans = 0
55+
for _ in range(2, k + 2):
56+
m_i, l_i = 4 * m_i + 5 * m_i + 20 * l_i, 4 * l_i + 5 * l_i + 4 * m_i
57+
ans += l_i
58+
59+
return ans
60+
61+
62+
if __name__ == "__main__":
63+
print(f"{solution() = }")

0 commit comments

Comments
 (0)