Cycle detection: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(One intermediate revision by one other user not shown)
Line 1:
{{draftDraft task}}
 
;Task:
Line 2,985:
/*-------------------------------------------------------------------*/
f: Return (arg(1)**2+1)// 255; /*define the function F*/</syntaxhighlight>
 
=={{header|RPL}}==
Translation of the Brent algorithm given in Wikipedia.
{{works with|HP|48}}
≪ 1 1 0 → f x0 power lam mu
≪ x0 DUP f EVAL <span style="color:grey">@ Main phase: search successive powers of two</span>
'''WHILE''' DUP2 ≠ '''REPEAT'''
'''IF''' power lam == '''THEN''' <span style="color:grey">@ time to start a new power of two?</span>
SWAP DROP DUP
2 'power' STO*
0 'lam' STO
'''END'''
f EVAL
1 'lam' STO+
'''END'''
DROP2 x0 DUP <span style="color:grey">@ Find the position of the first repetition of length λ</span>
0 lam 1 - '''START'''
f EVAL '''NEXT''' <span style="color:grey">@ distance between the hare and tortoise is now λ</span>
'''WHILE''' DUP2 ≠ '''REPEAT''' <span style="color:grey">@ the hare and tortoise move at same speed until they agree</span>
f EVAL SWAP
f EVAL SWAP
1 'mu' STO+
'''END'''
DROP2 lam mu
≫ ≫ '<span style="color:blue">CYCLEB</span>' STO
 
≪ SQ 1 + 255 MOD ≫ 0 <span style="color:blue">CYCLEB</span>
{{out}}
<pre>
2: 6
1: 2
</pre>
 
=={{header|Ruby}}==
Line 3,297 ⟶ 3,329:
=={{header|Wren}}==
Working from the code in the Wikipedia article:
<syntaxhighlight lang="ecmascriptwren">var brent = Fn.new { |f, x0|
var lam = 1
var power = 1
9,476

edits