# Calculating the stopping time function for the # original Collatz function.We kindly thank Gaston Gonnet # for allowing us to reproduce his Maple Code # here(see Maple Tech Newsletter Issue 6, Fall 1991). # Change the variables "lower" and "upper": lower := 1; upper := 100; DistTo1 := proc(n) local its, t, qt, u, rt, bits; if not type([args],[integer]) or n < 1 then ERROR(`invalid arguments`) fi; its := 0; t := n; do if t < 10 then RETURN( op(t,[0,1,7,2,5,8,16,3,19,6]) + its ) fi; bits := 3; while 2*length(pow2(bits)) < length(t) do bits := 2*bits od; qt := iquo(t,pow2(bits),'rt'); u := CollatzMod(rt,bits); its := its+u[2]+bits; t := qt*3^u[2]+u[1]; od; end: CollatzMod := proc(r,b) local b2, q1, q2, u1, u2, res; b2 := iquo(b,2); u1 := CollatzMod(irem(r,pow2(b2),'q1'),b2); u2 := CollatzMod(irem(q1*3^u1[2]+u1[1],pow2(b2),'q2'),b2); res := [q2*3^u2[2]+u2[1], u1[2]+u2[2]]; if b <= 12 then CollatzMod(args) := res fi; res end: CollatzMod(0,1) := [0,0]: CollatzMod(1,1) := [2,1]: CollatzMod(0,2) := [0,0]: CollatzMod(1,2) := [1,1]: CollatzMod(2,2) := [2,1]: CollatzMod(3,2) := [8,2]: CollatzMod(0,3) := [0,0]: CollatzMod(1,3) := [2,2]: CollatzMod(2,3) := [1,1]: CollatzMod(3,3) := [4,2]: CollatzMod(4,3) := [2,1]: CollatzMod(5,3) := [2,1]: CollatzMod(6,3) := [8,2]: CollatzMod(7,3) := [26,3]: pow2 := proc(b) option remember; if b <= 24 then 2^b else pow2(b/2)^2 fi end: plot( [seq( op( [i,DistTo1(i)]), i=lower..upper)], style=POINT); ### For use by authors only ### Experimental editor for the Maple Form Interface ### ### For use by authors only ### Experimental editor for the Maple Form Interface ###