aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-06-23 14:49:04 +0200
committerCharles Cabergs <me@cacharle.xyz>2021-06-23 14:49:04 +0200
commitd66aaa10226d2ccc94749fcdb255fa2eb969d0ed (patch)
treee05b68d1f4bf272fe185afae08130497a785cd74
parent85b66f8e64576ec3a17066bb1b920a5cd02a30c2 (diff)
downloadproject_euler-d66aaa10226d2ccc94749fcdb255fa2eb969d0ed.tar.gz
project_euler-d66aaa10226d2ccc94749fcdb255fa2eb969d0ed.tar.bz2
project_euler-d66aaa10226d2ccc94749fcdb255fa2eb969d0ed.zip
problem 85 in julia
-rw-r--r--julia/085-counting_rectangles.jl28
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