""" Another version, using a generator of Fibonacci "triples"...
Encapsulating a discovery -- not a proof. By David Koski (Python by K. Urner) Failure at around 37th power is due to floating point limitations. """ import unittest def fib_triple(a=0, b=1): """ >>> from anyproject import fib_triple >>> gem = fib_triple() >>> next(gem) (0, 1, 1) >>> next(gem) (1, 1, 2) >>> next(gem) (1, 2, 3) >>> next(gem) (2, 3, 5) >>> next(gem) (3, 5, 8) >>> gem = fib_triple(13,-8) >>> next(gem) (13, -8, 5) >>> next(gem) (-8, 5, -3) >>> next(gem) (5, -3, 2) >>> next(gem) (-3, 2, -1) >>> next(gem) (2, -1, 1) >>> next(gem) (-1, 1, 0) """ c = a + b while True: yield a, b, a + b a, b, c = b, a + b, b + c series = fib_triple(13,-8) # 13, -8, 5, -3, 2, -1, 1, 0 , 1, 2, 3, 5, 8 RT5 = pow(5, .5) # "square root" of five PHI = (1 + RT5)/2 # golden proportion def cool_formula(): """ Two fib numbers, two apart, plus the one in the middle * sqrt(5) all over 2, gives PHI to a power. Advancing all three sequences gives successive powers. """ while True: f0, f1, f2 = next(series) yield (f0 + f2 + f1 * RT5)/2.0 class TestPhi(unittest.TestCase): def test_loop(self): gem = cool_formula() for e in range(-6, 30): # adjust range (fails about 37th power) answer = next( gem ) self.assertAlmostEqual( PHI ** e, answer) if __name__ == "__main__": unittest.main()
_______________________________________________ Edu-sig mailing list Edu-sig@python.org https://mail.python.org/mailman/listinfo/edu-sig