Hailstone sequence: Difference between revisions

m
→‎optimized: changed comments and whitespace (indentations), elided one line of dead (useless) code.
m (→‎non-optimized: added/changed comments and whitespace.)
m (→‎optimized: changed comments and whitespace (indentations), elided one line of dead (useless) code.)
Line 5,929:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
hailstone: procedure expose @. !. hm; parse arg n 1 s 1 o,@.1 /*N,S,O: are the first1st arg.*/
@.1= do while @.n==0 /*handleloop while the specialresidual caseis forunknown. unity (1)*/
do while @.n==0 parse var n '' -1 L /*loop whileextract the residuallast isdecimal unknown.digit of N.*/
parse var n '' -1 L if !.L then n=n%2 /*extractN theis lasteven? decimal digit ofThen calculate fast ÷ N.*/
if !.L then n=n%2 else n=n*3 + 1 /*N? is even? odd ? Then calculate fast3*n ÷+ 1 */
else n s=n*3s + 1n /*? ? odd ?/* [↑] Then%: calculateis the 3*n +REXX 1integer division*/
s=s n end /*while*/ /* [↑] %:append is theN REXXto the integersequence divisionlist*/
ends=s @.n /*while*/ /* [↑] /*append the N number to thea sequence list.*/
s=s @.n @.o=subword(s, 2); parse var s _ r /*append theuse numbermemoization tofor athis sequencehailstone list#.*/
@.o=subword(s, 2); do while r\==''; parse var sr _ r /*useobtain memoizationthe fornext this hailstone #sequence. */
if @._\==0 then leave /*Was number already found? Return S.*/
h=!.h
do while if r\=='';_>hm parse var rthen _iterate r /*obtain the next subsequence. /*Is number out of range? Ignore it.*/
if @._\==0r then leave /*Was number already found? Return S /*assign subsequence number to array. */
if _>hm then iterate end /*Is number out of range? Ignore it.while*/
return s</lang>
@._=r /*assign subsequence number to array. */
end /*while*/
return s</lang>
'''output''' &nbsp; when using the default inputs:
<pre>