Cycle detection: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add Cowgol) |
Not a robot (talk | contribs) (Add APL) |
||
Line 173: | Line 173: | ||
Start Index : 2 |
Start Index : 2 |
||
Cycle : 101 2 5 26 167 95</pre> |
Cycle : 101 2 5 26 167 95</pre> |
||
=={{header|APL}}== |
|||
{{works with|Dyalog APL}} |
|||
<lang apl>brent←{ |
|||
f←⍺⍺ |
|||
lam←⊃{ |
|||
l p t h←⍵ |
|||
p=l: 1 (p×2) h (f h) ⋄ (l+1) p t (f h) |
|||
}⍣{=/2↓⍺} ⊢ 1 1 ⍵ (f ⍵) |
|||
mu←⊃{ |
|||
(⊃⍵+1),f¨1↓⍵ |
|||
}⍣{=/1↓⍺} ⊢ 0 ⍵ (f⍣lam⊢⍵) |
|||
mu lam |
|||
} |
|||
task←{ |
|||
seq←{f←⍺⍺ ⋄ (⊃⍺)↓{⍵,f⊃⌽⍵}⍣(1-⍨+/⍺)⊢⍵} |
|||
⎕←0 20 ⍺⍺ seq ⍵ ⍝ First 20 elements |
|||
⎕←(↑'Index' 'Length'),⍺⍺ brent ⍵ ⍝ Index and length of cycle |
|||
⎕←(⍺⍺ brent ⍺⍺ seq⊢)⍵ ⍝ Cycle |
|||
} |
|||
(255 | 1 + ⊢×⊢) task 3</lang> |
|||
{{out}} |
|||
<pre>3 10 101 2 5 26 167 95 101 2 5 26 167 95 101 2 5 26 167 95 |
|||
Index 2 |
|||
Length 6 |
|||
101 2 5 26 167 95</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Modula-2}} |
{{trans|Modula-2}} |