Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Project Euler] Add style improvements to problem 9 #3046

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions project_euler/problem_09/sol1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Problem Statement:
Problem 9: https://projecteuler.net/problem=9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
Expand All @@ -9,7 +10,7 @@
"""


def solution():
def solution() -> int:
"""
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following:
Expand All @@ -29,7 +30,7 @@ def solution():
return a * b * c


def solution_fast():
def solution_fast() -> int:
"""
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following:
Expand Down
15 changes: 8 additions & 7 deletions project_euler/problem_09/sol2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""
Problem Statement:
Problem 9: https://projecteuler.net/problem=9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
Expand All @@ -9,27 +10,27 @@
"""


def solution(n):
def solution(n: int = 1000) -> int:
"""
Return the product of a,b,c which are Pythagorean Triplet that satisfies
the following:
1. a < b < c
2. a**2 + b**2 = c**2
3. a + b + c = 1000
3. a + b + c = n

>>> solution(1000)
31875000
"""
product = -1
d = 0
candidate = 0
for a in range(1, n // 3):
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c"""
b = (n * n - 2 * a * n) // (2 * n - 2 * a)
c = n - a - b
if c * c == (a * a + b * b):
d = a * b * c
if d >= product:
product = d
candidate = a * b * c
if candidate >= product:
product = candidate
return product


Expand Down
4 changes: 2 additions & 2 deletions project_euler/problem_09/sol3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Problem Statement:
Problem 9: https://projecteuler.net/problem=9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

Expand All @@ -12,7 +12,7 @@
"""


def solution():
def solution() -> int:
"""
Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following:
Expand Down