Hailstone sequence: Difference between revisions
Content added Content deleted
Line 771: | Line 771: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
<lang APL> |
<lang APL> |
||
⍝ 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 |
|||
} |
|||
⍝ tradfn version: |
|||
∇seq←hailstone n;next |
|||
⍝ Returns the hailstone sequence for a given number |
⍝ Returns the hailstone sequence for a given number |
||
Line 779: | Line 788: | ||
n←next[1+2|n] ⍝ Pick the appropriate next step |
n←next[1+2|n] ⍝ Pick the appropriate next step |
||
seq,←n ⍝ Append that to the sequence |
seq,←n ⍝ Append that to the sequence |
||
:EndWhile |
:EndWhile |
||
∇ |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<lang APL> |
<lang APL> dfnHailstone 5 |
||
5 16 8 4 2 1 |
|||
5↑hailstone 27 |
|||
27 82 41 124 62 |
27 82 41 124 62 |
||
¯5↑hailstone 27 |
¯5↑hailstone 27 |