60 lines
1016 B
Plaintext
60 lines
1016 B
Plaintext
local fn Hailstone( n as NSInteger ) as NSInteger
|
|
NSInteger count = 1
|
|
|
|
while ( n != 1 )
|
|
if ( n and 1 ) == 1
|
|
n = n * 3 + 1
|
|
count++
|
|
end if
|
|
n = n / 2
|
|
count++
|
|
wend
|
|
end fn = count
|
|
|
|
|
|
|
|
void local fn PrintHailstone( n as NSInteger )
|
|
NSInteger count = 1, col = 1
|
|
|
|
print "Sequence for number "; n; ":" : print
|
|
print using "########"; n;
|
|
|
|
col = 2
|
|
while ( n != 1 )
|
|
if ( n and 1 ) == 1
|
|
n = n * 3 + 1
|
|
count++
|
|
else
|
|
n = n / 2
|
|
count++
|
|
end if
|
|
print using "########"; n;
|
|
if col == 10 then print : col = 1 else col++
|
|
wend
|
|
|
|
print : print
|
|
print "Sequence length = "; count
|
|
end fn
|
|
|
|
window 1, @"Hailstone Sequence", ( 0, 0, 620, 400 )
|
|
|
|
NSInteger n, seq_num, x, max_x, max_seq
|
|
|
|
seq_num = 27
|
|
|
|
print
|
|
fn PrintHailstone( seq_num )
|
|
print
|
|
|
|
for x = 1 to 100000
|
|
n = fn Hailstone( x )
|
|
if n > max_seq
|
|
max_x = x
|
|
max_seq = n
|
|
end if
|
|
next
|
|
|
|
print "The longest sequence is for "; max_x; ", it has a sequence length of "; max_seq; "."
|
|
|
|
HandleEvents
|