How do I write this equation in Python? -
i don't know how write correctly. how tried:
def is_cardano_triplet(a, b, c): f = lambda x: x ** 1. / 2 g = lambda x: x ** 1. / 3 return g(a + b*f(c)) + g(a - b*f(c)) == 1 print is_cardano_triplet(2,1,5) # should true
i should true
2, 1, 5
, i'm not. what's wrong function?
doing bit of research and calculations, solving equation variable c, i found out that
and therefore
now, due floating point arithmetic being imprecise on binary-based systems known reasons, first formula pretty hard compute precisely. however, the second 1 easier compute without floating point precision errors since doesn't involve irrational functions , a
, b
, c
integers.
here's smart solution:
def is_cardano_triplet(a, b, c): return (a + 1)**2 * (8*a - 1) - 27*b**2*c == 0 >>> is_cardano_triplet(2, 1, 5) true
Comments
Post a Comment