fork download
  1. import math
  2.  
  3. def cos(x, n):
  4. formula = lambda k: (-1)**k * x**(2*k) / math.factorial(2*k)
  5. soma = 0
  6.  
  7. for k in range(n+1):
  8. soma += formula(k)
  9.  
  10. return soma
  11.  
  12. assert math.isclose(cos(0.5, 10), math.cos(0.5))
  13. assert math.isclose(cos(-0.5, 10), math.cos(-0.5))
  14. assert math.isclose(cos(0.0, 10), math.cos(0.0))
  15. assert math.isclose(cos(1.0, 10), math.cos(1.0))
Success #stdin #stdout 0.02s 9240KB
stdin
Standard input is empty
stdout
Standard output is empty