Return index of for loop



Hi,

I want to make function which computes period.
Period is an integer number which shows how many iterates of
rational number ( = n/d ) under doubling_map is needed to get the same 
value.
I made 2 functions :
- first looks nice but not works ( it seems that maxima does not see 
index of for loop out of loop.
- second function uses go-loop. It works, but ...

Can it be done better ?

Adam Majewski


======== code ==================



doubling_map(n,d):=mod(2*n,d);


/* first function does not work,
gives i as an output */
/* Give Period Of rational external Angle
under doubling map */
GivePeriodOfAngle(n0,d):=block(
[ni:doubling_map(n0,d)],
for i:1 step 1 thru 200 unless (n0=ni) do ni:doubling_map(ni,d),
i
);


/* this second function works */
GiveP(n0,d):=block(
[ni:n0,i:0],
block(
   loop,
   ni:doubling_map(ni,d),
   i:i+1,
   if i<100 and not (n0=ni) then go(loop)
),
if (n0=ni)
	then i
	else 0
);