#!/usr/bin/env python

import sys, math

def get_cont_fractions (p, maxlen=10):
   c_fract = []
   p0 = 1
   q0 = 0
   p1 = math.floor (p)
   q1 = 1

   c_fract.append (p1)
   d = (p - p1)
   if math.fabs (d) <= 0.00000001:
      return c_fract

   p = 1.0 / d

   while len (c_fract) < maxlen:
      c = int (math.floor (p))
      
      p0, p1 = p1, c * p1 + p0
      q0, q1 = q1, c * q1 + q0

      print "%d/%d" % (p0, q0)
      c_fract.append (c)
      d = (p - c)
      if math.fabs (d) <= 0.00000001:
         break

      p = 1.0 / d

   return c_fract


def get_frac_from_cont_fractions (c_fract):
   if not c_fract:
      raise "Empty List as Continuos Fractions"

   c_f = c_fract[:]
   c_f.reverse ()
   a, b = 0, 1
   for i in c_f:
      a, b = b, i * b + a

   return b, a
      
   
def gcd (a, b):
   if a < b:
      a, b = b, a

   c = b

   while a % b != 0:
      c = a % b
      a, b = b, c

   return c

   
if __name__=='__main__':
   p = math.tan (30 * math.pi / 180)
   p = (1 + 5.0**0.5)/2
   p = 1.618
   p = 15000./0x15cc
   p = 2**.5

   if len (sys.argv) > 1:
      p = float (sys.argv[1])

   if len (sys.argv) > 2:
      p = float (sys.argv[1]) / float (sys.argv[2])

   for n in range (20):
      cf = get_cont_fractions (p, n+1)
      a, b = get_frac_from_cont_fractions (cf)
      print "%15s = %.10f" % ("%d/%d" % (a, b), float (a) / b)

