Skip to content

Add style improvements for Project Euler problem 8 #3001

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

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
12 changes: 7 additions & 5 deletions project_euler/problem_08/sol1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 8: https://projecteuler.net/problem=8

The four adjacent digits in the 1000-digit number that have the greatest
product are 9 × 9 × 8 × 9 = 5832.

Expand Down Expand Up @@ -50,21 +52,21 @@
71636269561882670428252483600823257530420752963450"""


def solution(n):
def solution(n: str = N) -> int:
"""Find the thirteen adjacent digits in the 1000-digit number n that have
the greatest product and returns it.

>>> solution(N)
23514624000
"""
LargestProduct = -sys.maxsize - 1
largest_product = -sys.maxsize - 1
for i in range(len(n) - 12):
product = 1
for j in range(13):
product *= int(n[i + j])
if product > LargestProduct:
LargestProduct = product
return LargestProduct
if product > largest_product:
largest_product = product
return largest_product


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion project_euler/problem_08/sol2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 8: https://projecteuler.net/problem=8

The four adjacent digits in the 1000-digit number that have the greatest
product are 9 × 9 × 8 × 9 = 5832.

Expand Down Expand Up @@ -53,7 +55,7 @@
)


def solution(n):
def solution(n: str = N) -> int:
"""Find the thirteen adjacent digits in the 1000-digit number n that have
the greatest product and returns it.

Expand Down
27 changes: 18 additions & 9 deletions project_euler/problem_08/sol3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 8: https://projecteuler.net/problem=8

The four adjacent digits in the 1000-digit number that have the greatest
product are 9 × 9 × 8 × 9 = 5832.

Expand Down Expand Up @@ -50,32 +52,39 @@
71636269561882670428252483600823257530420752963450"""


def streval(s: str) -> int:
ret = 1
for it in s:
ret *= int(it)
return ret
def str_eval(s: str) -> int:
"""Returns product of digits in given string n

>>> str_eval("987654321")
362880
>>> str_eval("22222222")
256
"""
product = 1
for digit in s:
product *= int(digit)
return product


def solution(n: str) -> int:
def solution(n: str = N) -> int:
"""Find the thirteen adjacent digits in the 1000-digit number n that have
the greatest product and returns it.

>>> solution(N)
23514624000
"""
LargestProduct = -sys.maxsize - 1
largest_product = -sys.maxsize - 1
substr = n[:13]
cur_index = 13
while cur_index < len(n) - 13:
if int(n[cur_index]) >= int(substr[0]):
substr = substr[1:] + n[cur_index]
cur_index += 1
else:
LargestProduct = max(LargestProduct, streval(substr))
largest_product = max(largest_product, str_eval(substr))
substr = n[cur_index : cur_index + 13]
cur_index += 13
return LargestProduct
return largest_product


if __name__ == "__main__":
Expand Down