-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproblem85.py
More file actions
29 lines (26 loc) · 818 Bytes
/
problem85.py
File metadata and controls
29 lines (26 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Counting Rectangles
Project Euler Problem #85
by Muaz Siddiqui
By counting carefully it can be seen that a rectangular grid measuring 3 by 2
contains eighteen rectangles:
Although there exists no rectangular grid that contains exactly two million
rectangles, find the area of the grid with the nearest solution.
"""
from euler_helpers import timeit
# The formula f(x,y) = ((x^2 +x)*(y^2+y))/4 gives the number of rectangles in a
# rectangle of dimensions x,y.
@timeit
def answer():
min_ = 2000000
x = 2
y = min_//6
while x <= y:
# Finds nearest to 2 million rectangles
difference = abs(x*(x+1)* y*(y+1)/4 - 2000000)
if difference < min_:
area = x * y
min_ = difference
x += 2
y = int((2000000*4/(x*(x+1)))**0.5)
return area