We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def tan_add(t,u):
// compute tan(a+b) given tan(a) = t and tan(b) = u
return (a + b) / (1 - a*b)
def tan(n,t):
// compute tan(n*a) given tan(a) = t
if n == 1:
return t
else if n % 2 == 0:
result = tan(n/2,t)
return tan_add(result, result)
else:
result = tan(n-1,t)
return tan_add(result, t)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Simple One
You are viewing a single comment's thread. Return to all comments →
def tan_add(t,u): // compute tan(a+b) given tan(a) = t and tan(b) = u return (a + b) / (1 - a*b)
def tan(n,t): // compute tan(n*a) given tan(a) = t if n == 1: return t else if n % 2 == 0: result = tan(n/2,t) return tan_add(result, result) else: result = tan(n-1,t) return tan_add(result, t)