about | help | code help+videos | done | prefs |
For this method, you are trying to approximate the area under the curve of y = -ax^2+b, but above the x-axis. This curve has roots at x=sqrt(b/a) and x=-sqrt(b/a). The y-intercept is (0,b). This means that the curve will fit inside a rectangle that goes from (-sqrt(b/a),0) to (sqrt(b/a),0) with a height of b. The area of this rectangle is 2*b*sqrt(b/a). You can approximate the area under this curve by using the Monty Carlo method. This method involves randomly picking points within the rectangle and finding what percentage of those points are below the curve. Let's say that 60% of the points are below the curve. That means that 60% of the area of the rectangle should be below the curve. This means that the area bound by the curve and the x-axis will be approximately 60% of the area of the rectangle. Of course this is just an example. The more random points you pick the more accurate your approximation will be. Since this is a CodingBat problem using a random number generator, I had to make it "good enough" so that the test data will work! So once you get your approximation, round it to the nearest integer before you return it. If you get answers that are "close" to mine, but are not quite right, you probably just need to pick more random points. The Monty Carlo method is just a suggestion. If you have another clever way to do this problem you are welcome to do it that way. You just can't hard-code the answers! You must have some "algorithmic" approach. I actually one-lined this one and got it right on the first try!!! (But that was NOT using the Monty Carlo method) quiz2020_12_02_HL_montyCarloMethod(5.3, 66.8) → 316 quiz2020_12_02_HL_montyCarloMethod(1.0, 45.5) → 409 quiz2020_12_02_HL_montyCarloMethod(4.2, 16.1) → 42 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 290
Copyright Nick Parlante 2017 - privacy