Add style improvements to Project Euler problem 9 (#3046)

This commit is contained in:
Edward Nuno 2020-10-08 04:21:32 -07:00 committed by GitHub
parent 7d54056497
commit 899870be4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 12 deletions

View File

@ -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 Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2 a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
@ -9,7 +10,7 @@ Find the product abc.
""" """
def solution(): def solution() -> int:
""" """
Returns the product of a,b,c which are Pythagorean Triplet that satisfies Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following: the following:
@ -29,7 +30,7 @@ def solution():
return a * b * c return a * b * c
def solution_fast(): def solution_fast() -> int:
""" """
Returns the product of a,b,c which are Pythagorean Triplet that satisfies Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following: the following:

View File

@ -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 Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c^2 a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
@ -9,27 +10,27 @@ Find the product abc.
""" """
def solution(n): def solution(n: int = 1000) -> int:
""" """
Return the product of a,b,c which are Pythagorean Triplet that satisfies Return the product of a,b,c which are Pythagorean Triplet that satisfies
the following: the following:
1. a < b < c 1. a < b < c
2. a**2 + b**2 = c**2 2. a**2 + b**2 = c**2
3. a + b + c = 1000 3. a + b + c = n
>>> solution(1000) >>> solution(1000)
31875000 31875000
""" """
product = -1 product = -1
d = 0 candidate = 0
for a in range(1, n // 3): for a in range(1, n // 3):
"""Solving the two equations a**2+b**2=c**2 and a+b+c=N eliminating c""" """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) b = (n * n - 2 * a * n) // (2 * n - 2 * a)
c = n - a - b c = n - a - b
if c * c == (a * a + b * b): if c * c == (a * a + b * b):
d = a * b * c candidate = a * b * c
if d >= product: if candidate >= product:
product = d product = candidate
return product return product

View File

@ -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, A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
@ -12,7 +12,7 @@ Find the product abc.
""" """
def solution(): def solution() -> int:
""" """
Returns the product of a,b,c which are Pythagorean Triplet that satisfies Returns the product of a,b,c which are Pythagorean Triplet that satisfies
the following: the following: