def cycle_length(f, x0, nmax=None, values=False):
 nmax = int((nmax or 0))
 power = lam = 1
 (tortoise, hare) = (x0, f(x0))
 i = 0
 while ((tortoise != hare) and ((not nmax) or (i < nmax))):
  i += 1
  if (power == lam):
   tortoise = hare
   power *= 2
   lam = 0
  if values:
   (yield hare)
  hare = f(hare)
  lam += 1
 if (nmax and (i == nmax)):
  if values:
   return
  else:
   (yield (nmax, None))
   return
 if (not values):
  mu = 0
  tortoise = hare = x0
  for i in range(lam):
   hare = f(hare)
  while (tortoise != hare):
   tortoise = f(tortoise)
   hare = f(hare)
   mu += 1
  if mu:
   mu -= 1
  (yield (lam, mu))

def cycle_length ( f , x0 , nmax = None , values = False ) : nmax = int ( ( nmax or 0 ) ) power = lam = 1 ( tortoise , hare ) = ( x0 , f ( x0 ) ) i = 0 while ( ( tortoise != hare ) and ( ( not nmax ) or ( i < nmax ) ) ) : i += 1 if ( power == lam ) : tortoise = hare power *= 2 lam = 0 if values : ( yield hare ) hare = f ( hare ) lam += 1 if ( nmax and ( i == nmax ) ) : if values : return else : ( yield ( nmax , None ) ) return if ( not values ) : mu = 0 tortoise = hare = x0 for i in range ( lam ) : hare = f ( hare ) while ( tortoise != hare ) : tortoise = f ( tortoise ) hare = f ( hare ) mu += 1 if mu : mu -= 1 ( yield ( lam , mu ) )
for a given iterated sequence .