Cycle detection: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, used a template for an output section, simplified some programs.
m (→‎{{header|REXX}}: added/changed whitespace and comments, used a template for an output section, simplified some programs.)
Line 2,002:
===Brent's algorithm===
<lang rexx>/*REXX program detects a cycle in an iterated function [F] using Brent's algorithm. */
init= 3; @$= init
do until length(@$) > 79; @$= @$ f( word(@$, words(@$) ) )
end /*until*/
say ' original list=' @$ ... /*display original number list*/
call Brent init /*invoke Brent algorithm for F*/
parse var result cycle idx /*get 2 values returned from F*/
say 'numbers in list=' words(@$) /*display number of numbers. */
say ' cycle length =' cycle /*display the cycle to term*/
say ' start index =' idx " ◄─── zero index" /* " " index " " */
say 'cycle sequence =' subword(@$, idx+1, cycle) /* " " sequence " " */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,022:
end
hare= f(hare)
#= # +1 1 /*bump the lambda count value.*/
end /*while*/
hare= x0
Line 2,035:
/*──────────────────────────────────────────────────────────────────────────────────────*/
f: return ( arg(1) **2 + 1) // 255 /*this defines/executes the function F.*/</lang>
'''{{out|output''' |text=&nbsp; when using the defaults:}}
<pre>
original list= 3 10 101 2 5 26 167 95 101 2 5 26 167 95 101 2 5 26 167 95 101 2 5 26 167 95 101 ...
Line 2,046:
===sequential search algorithm===
<lang rexx>/*REXX program detects a cycle in an iterated function [F] using a sequential search. */
x= 3; @$=x x /*initial couple of variables*/
do until cycle\==0; x= f(x) /*calculate another number. */
cycle= wordpos(x, @$) /*This could be a repeat. */
@= @ x $= $ x /*append number to @$ list.*/
end /*until*/
#= words(@) - cycle say ' original list=' $ ... /*computedisplay the cycle lengthsequence. */
say 'numbers originalin list=' @ ... words($) /*display thenumber sequenceof numbers. */
say 'numbers in listcycle length =' words(@$) - cycle /*display numberthe cycle of numbers.to term*/
say ' cycle length =' # /*display the cycle to term*/
say ' start index =' cycle - 1 " ◄─── zero based" /* " " index " " */
say 'cycle sequence =' subword(@$, cycle, #words($)-cycle) /* " " sequence " " */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,071 ⟶ 2,070:
===hash table algorithm===
This REXX version is a lot faster &nbsp; (than the sequential search algorithm) &nbsp; if the &nbsp; ''cycle length'' &nbsp; and/or &nbsp; ''start index'' &nbsp; is large.
<lang rexx>/*REXX program detects a cycle in an iterated function [F] using a hash table. */
say!.= '.; cycle length !.x=' 1; # x= 3; /*display the$= cyclex /*assign initial value. to term*/
x=3; @=x; !.=.; !.x=1
do n#=1+words(@$); x= f(x); @$= @$ x /*add the number to list. */
if !.x\==. then leave /*A repeat? Then leave. */
!.x= n# /*N: numbers in @ $ list.*/
end /*n#*/
say ' original list=' @$ ... /*maybe display the list. */
say 'numbers in list=' n# /*display number of nums. */
say ' cycle length =' n# - !.x /* " " cycle. */
say ' start index =' !.x - 1 '" ◄─── zero based'" /* " " index. */
say 'cycle sequence =' subword(@$, !.x, n# - !.x) /* " " sequence. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,106 ⟶ 2,105:
say ' divisor =' "{2**"power'}-1 = ' divisor /* " " divisor. " " " */
say
x=3; @$=x; @@$$=; m=100; !.=.; !.x=1 /*M: maximum numbers to display.*/
 
do n=1+words(@$); x= f(x); @@$$=@@$$ x
if n//2000==0 then do; @$=@$ @@$$; @@$$=; end /*Is a 2000th N? Rejoin.*/
if !.x\==. then leave /*is this number a repeat? Leave*/
!.x= n
end /*n*/ /*N: is the size of @$ list.*/
@$= space(@$ @@$$) /*append residual numbers to @$ */
if n<m then say ' original list=' @$ ... /*maybe display the list to term.*/
say 'numbers in list=' n /*display number of numbers. */
say ' cycle length =' n - !.x /*display the cycle to the term. */
say ' start index =' !.x - 1 " ◄─── zero based" /*show the index.*/
if n<m then say 'cycle sequence =' subword(@$, !.x, n- !.x) /*maybe display the sequence*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/