from math import factorial, radians, isclose, sin
def seno(x, n=7):
x = radians(x)
def termo_geral(x, i):
return ((-1)**i / factorial(2*i+1)) * (x**(2*i+1))
def termos(x, n):
for i in range(n):
yield termo_geral(x, i)
return sum(termos(x, n))
for angle in {0, 30, 45, 60, 90, 120, 180, 230, 270, 320, 360}:
assert isclose(
seno(angle, 21),
sin(radians(angle)),
abs_tol=1e-5
)
ZnJvbSBtYXRoIGltcG9ydCBmYWN0b3JpYWwsIHJhZGlhbnMsIGlzY2xvc2UsIHNpbgoKZGVmIHNlbm8oeCwgbj03KToKICAgIHggPSByYWRpYW5zKHgpCgogICAgZGVmIHRlcm1vX2dlcmFsKHgsIGkpOgogICAgICAgIHJldHVybiAoKC0xKSoqaSAvIGZhY3RvcmlhbCgyKmkrMSkpICogKHgqKigyKmkrMSkpCgogICAgZGVmIHRlcm1vcyh4LCBuKToKICAgICAgICBmb3IgaSBpbiByYW5nZShuKToKICAgICAgICAgICAgeWllbGQgdGVybW9fZ2VyYWwoeCwgaSkKCiAgICByZXR1cm4gc3VtKHRlcm1vcyh4LCBuKSkKCmZvciBhbmdsZSBpbiB7MCwgMzAsIDQ1LCA2MCwgOTAsIDEyMCwgMTgwLCAyMzAsIDI3MCwgMzIwLCAzNjB9OgogICAgYXNzZXJ0IGlzY2xvc2UoCiAgICAgICAgc2VubyhhbmdsZSwgMjEpLCAKICAgICAgICBzaW4ocmFkaWFucyhhbmdsZSkpLCAKICAgICAgICBhYnNfdG9sPTFlLTUKICAgICk=