31 lines
876 B
Plaintext
31 lines
876 B
Plaintext
include c:\cxpl\codes; \intrinsic 'code' declarations
|
|
int Seq(1000); \more than enough for longest sequence
|
|
|
|
func Hailstone(N); \Return length of Hailstone sequence starting at N
|
|
int N; \ also fills Seq array with sequence
|
|
int I;
|
|
[I:= 0;
|
|
loop [Seq(I):= N; I:= I+1;
|
|
if N=1 then return I;
|
|
N:= if N&1 then N*3+1 else N/2;
|
|
];
|
|
];
|
|
|
|
int N, SN, Len, MaxLen;
|
|
[Len:= Hailstone(27);
|
|
Text(0, "27's Hailstone length = "); IntOut(0, Len); CrLf(0);
|
|
|
|
Text(0, "Sequence = ");
|
|
for N:= 0 to 3 do [IntOut(0, Seq(N)); ChOut(0, ^ )];
|
|
Text(0, "... ");
|
|
for N:= Len-4 to Len-1 do [IntOut(0, Seq(N)); ChOut(0, ^ )];
|
|
CrLf(0);
|
|
|
|
MaxLen:= 0;
|
|
for N:= 1 to 100_000-1 do
|
|
[Len:= Hailstone(N);
|
|
if Len > MaxLen then [MaxLen:= Len; SN:= N]; \save N with max length
|
|
];
|
|
IntOut(0, SN); Text(0, "'s Hailstone length = "); IntOut(0, MaxLen);
|
|
]
|