Cycle detection: Difference between revisions

Add APL
(Add Cowgol)
(Add APL)
Line 173:
Start Index : 2
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}}==
{{trans|Modula-2}}
2,093

edits