From c92327214209c0a989d0c1b7e364ebccfeb7fd6f Mon Sep 17 00:00:00 2001 From: osoba Date: Thu, 8 Oct 2020 06:46:32 +0200 Subject: [PATCH 1/2] add problem 85 --- project_euler/problem_85/sol1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 project_euler/problem_85/sol1.py diff --git a/project_euler/problem_85/sol1.py b/project_euler/problem_85/sol1.py new file mode 100644 index 000000000000..8b9dd812b6f6 --- /dev/null +++ b/project_euler/problem_85/sol1.py @@ -0,0 +1,18 @@ +import math + +def solution() -> int: + goal = 8000000 + root = int(math.sqrt(8000000)+1) + distance, resa, resb = goal, 0, 0 + + for a in range(1, root+1): + for b in range(1, math.ceil(root/a)+1): + current_distance = abs((a*b+a)*(a*b+b) - goal) + if current_distance < distance: + distance = current_distance + resa, resb = a, b + + return resa*resb + +if __name__ == "__main__": + print("Area of the grid is ", solution()) From 43e98668193c7937a070d31cb32130539738c601 Mon Sep 17 00:00:00 2001 From: osoba Date: Thu, 8 Oct 2020 19:07:15 +0200 Subject: [PATCH 2/2] code reformat using black --- project_euler/problem_85/sol1.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/project_euler/problem_85/sol1.py b/project_euler/problem_85/sol1.py index 8b9dd812b6f6..cd148c009925 100644 --- a/project_euler/problem_85/sol1.py +++ b/project_euler/problem_85/sol1.py @@ -1,18 +1,20 @@ import math + def solution() -> int: - goal = 8000000 - root = int(math.sqrt(8000000)+1) - distance, resa, resb = goal, 0, 0 + goal = 8000000 + root = int(math.sqrt(8000000) + 1) + distance, resa, resb = goal, 0, 0 + + for a in range(1, root + 1): + for b in range(1, math.ceil(root / a) + 1): + current_distance = abs((a * b + a) * (a * b + b) - goal) + if current_distance < distance: + distance = current_distance + resa, resb = a, b - for a in range(1, root+1): - for b in range(1, math.ceil(root/a)+1): - current_distance = abs((a*b+a)*(a*b+b) - goal) - if current_distance < distance: - distance = current_distance - resa, resb = a, b + return resa * resb - return resa*resb if __name__ == "__main__": print("Area of the grid is ", solution())