aboutsummaryrefslogtreecommitdiff
path: root/julia/085-counting_rectangles.jl
blob: 004cdbd006d7b80e8d98b18747021326d1ef8ef6 (plain)
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
###
# Counting rectangles
# Problem 85
#
# 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.
###


# blackpenredpend: https://www.youtube.com/watch?v=Uq9OXC0Gzgw

using Base.Iterators


const TARGET_RECTANGLES = 2_000_000

combinations_2(n) = (n * (n - 1)) / 2
dist = Inf
for width in countfrom(1), height in 1:width
    count = combinations_2(width + 1) * combinations_2(height + 1)
    global dist = min(dist, abs(TARGET_RECTANGLES - count))
    if dist < 3  # I just tested a bunch of different threshold
        println(width * height)
        break
    end
end