48 lines
1.3 KiB
Text
48 lines
1.3 KiB
Text
Module hailstone.Task {
|
|
hailstone=lambda (n as long)->{
|
|
=lambda n (&val) ->{
|
|
if n=1 then =false: exit
|
|
=true
|
|
if n mod 2=0 then n/=2 : val=n: exit
|
|
n*=3 : n++: val=n
|
|
}
|
|
}
|
|
count=Lambda (n as long) ->{
|
|
m=lambda n ->{
|
|
if n=1 then =false: exit
|
|
=true :if n mod 2=0 then n/=2 :exit
|
|
n*=3 : n++
|
|
}
|
|
c=1
|
|
While m() {c++}
|
|
=c
|
|
|
|
}
|
|
k=Hailstone(27)
|
|
counter=1
|
|
x=0
|
|
Print 27,
|
|
While k(&x) {
|
|
counter++
|
|
Print x,
|
|
if counter=4 then exit
|
|
}
|
|
Print
|
|
Flush ' empty current stack
|
|
While k(&x) {
|
|
counter++
|
|
data x ' send to end of stack -used as FIFO
|
|
if stack.size>4 then drop
|
|
}
|
|
\\ [] return a stack object and leave empty current stack
|
|
\\ Print use automatic iterator to print all values in columns.
|
|
Print []
|
|
Print "counter:";counter
|
|
m=0
|
|
For i=2 to 99999 {
|
|
m1=max.data(count(i), m)
|
|
if m1<>m then m=m1: im=i
|
|
}
|
|
Print Format$("Number {0} has then longest hailstone sequence of length {1}", im, m)
|
|
}
|
|
hailstone.Task
|