From 807244c81a9c7cdad56788008a2a842945b5d441 Mon Sep 17 00:00:00 2001 From: Colin Nielsen <33375223+colinnielsen@users.noreply.github.com> Date: Thu, 3 Sep 2020 09:49:51 -0600 Subject: [PATCH] fix: update formatting, add image + solution (#38907) * fix: update formatting, add image + solution * fix: change img src and add suggestions Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com> --- ...em-199-iterative-circle-packing.english.md | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-199-iterative-circle-packing.english.md b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-199-iterative-circle-packing.english.md index 7ea4bc51bb2..1b4f2b099b3 100644 --- a/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-199-iterative-circle-packing.english.md +++ b/curriculum/challenges/english/10-coding-interview-prep/project-euler/problem-199-iterative-circle-packing.english.md @@ -10,12 +10,11 @@ forumTopicId: 301837
Three circles of equal radius are placed inside a larger circle such that each pair of circles is tangent to one another and the inner circles do not overlap. There are four uncovered "gaps" which are to be filled iteratively with more tangent circles. - +a diagram of non-overlapping concentric circles At each iteration, a maximally sized circle is placed in each gap, which creates more gaps for the next iteration. After 3 iterations (pictured), there are 108 gaps and the fraction of the area which is not covered by circles is 0.06790342, rounded to eight decimal places. - -What fraction of the area is not covered by circles after 10 iterations? +What fraction of the area is not covered by circles after `n` iterations? Give your answer rounded to eight decimal places using the format x.xxxxxxxx .
@@ -29,8 +28,12 @@ Give your answer rounded to eight decimal places using the format x.xxxxxxxx . ```yml tests: - - text: euler199() should return 0.00396087. - testString: assert.strictEqual(euler199(), 0.00396087); + - text: iterativeCirclePacking(10) should return a number. + testString: assert(typeof iterativeCirclePacking(10) === 'number'); + - text: iterativeCirclePacking(10) should return 0.00396087. + testString: assert.strictEqual(iterativeCirclePacking(10), 0.00396087); + - text: iterativeCirclePacking(3) should return 0.06790342. + testString: assert.strictEqual(iterativeCirclePacking(3), 0.06790342); ``` @@ -42,12 +45,12 @@ tests:
```js -function euler199() { +function iterativeCirclePacking(n) { // Good luck! return true; } -euler199(); +iterativeCirclePacking(10); ```
@@ -60,7 +63,26 @@ euler199();
```js -// solution required +function iterativeCirclePacking(n) { + let k1 = 1; + let k0 = k1 * (3 - 2 * Math.sqrt(3)); + let a0 = 1 / (k0 * k0); + let a1 = 3 / (k1 * k1); + a1 += 3 * getArea(k0, k1, k1, n); + a1 += getArea(k1, k1, k1, n); + let final = ((a0 - a1) / a0).toFixed(8); + + return parseFloat(final); + function getArea(k1, k2, k3, depth) { + if (depth == 0) return 0.0; + let k4 = k1 + k2 + k3 + 2 * Math.sqrt(k1 * k2 + k2 * k3 + k3 * k1); + let a = 1 / (k4 * k4); + a += getArea(k1, k2, k4, depth - 1); + a += getArea(k2, k3, k4, depth - 1); + a += getArea(k3, k1, k4, depth - 1); + return a; + } +} ```