diff options
| -rw-r--r-- | julia/085-counting_rectangles.jl | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/julia/085-counting_rectangles.jl b/julia/085-counting_rectangles.jl new file mode 100644 index 0000000..004cdbd --- /dev/null +++ b/julia/085-counting_rectangles.jl @@ -0,0 +1,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 |
