Problem 587

completed October 30, 2020

Comments

I do love geometry and trigonometry! This one really got my brain working and it was really fun to do. I drew many many pictures and spent time relearning all I used to know of trig. Despite this, I kept getting the wrong answer. I checked my work about 5 times and everytime saw that I was doing it correctly. I finally realized that it was the value for l_section_area that I’d miscalculated. Here’s all this hard trig and I’m fine, but some simple sibtraction I couldn’t even do! Upon fixing that I was able to get the answer super fast!

Code

import math

l_section_area = 1 - (math.pi / 4)


def solve(n):
    k = 1
    while percent(k) > n:
        k += 1
    return k


def percent(n):
    theta = math.atan(1 / n)
    alpha = math.asin((n - 1) / (n ** 2 + 1) ** 0.5)
    beta = math.pi / 2 - theta - alpha

    segment_area = (beta - math.sin(beta)) / 2
    triangle_area = (1 - math.cos(beta)) / 2
    concave_triangle_area = triangle_area - segment_area

    return concave_triangle_area / l_section_area


if __name__ == '__main__':
    import sys
    n = eval(sys.argv[1])
    print(solve(n))

Tests

import pytest
from problem import solve, percent

_test_percent = (
        (1, 0.50),
        (2, 0.36462616929172803),
)

@pytest.mark.parametrize('n,expect', _test_percent)
def test_percent(n, expect):
    assert expect == percent(n)

_test_solve = (
        (0.10, 15),
)

@pytest.mark.parametrize('n,expect', _test_solve)
def test_solve(n, expect):
    assert expect == solve(n)