From d66aaa10226d2ccc94749fcdb255fa2eb969d0ed Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 23 Jun 2021 14:49:04 +0200 Subject: problem 85 in julia --- julia/085-counting_rectangles.jl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 julia/085-counting_rectangles.jl (limited to 'julia') 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 -- cgit