RosettaCodeData/Task/Hailstone-sequence/APL/hailstone-sequence-1.apl
2023-07-01 13:44:08 -04:00

18 lines
574 B
APL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

⍝ recursive dfn:
dfnHailstone{
c ⍝ last element
1=c:1 ⍝ if it is 1, stop.
,(1+2|c)(c÷2)(1+3×c) ⍝ otherwise pick the next step, and append the result of the recursive call
}
⍝ tradfn version:
seqhailstone n;next
⍝ Returns the hailstone sequence for a given number
seqn ⍝ Init the sequence
:While n1
next(n÷2) (1+3×n) ⍝ Compute both possibilities
nnext[1+2|n] ⍝ Pick the appropriate next step
seq,n ⍝ Append that to the sequence
:EndWhile