27 lines
730 B
Plaintext
27 lines
730 B
Plaintext
Array g[2]
|
|
|
|
Func Collatz(n, d) =
|
|
{Runs the Collatz procedure for the number n and returns the number of steps.}
|
|
{If d is nonzero, prints the terms in the sequence.}
|
|
steps := 1;
|
|
while n>1 do
|
|
if n|2=0 then n:=n/2 else n:=3n+1 fi;
|
|
if d then !!n fi;
|
|
steps := steps + 1
|
|
od;
|
|
steps.
|
|
|
|
Function LongestTo(n) =
|
|
{Finds the number up to n for which the Collatz algorithm takes the most number of steps.}
|
|
{The result is stored in the array [g]: g[1] is the number, g[2] is how many steps it takes.}
|
|
champ:=0;
|
|
record:=0;
|
|
for i = 1, n do
|
|
q:=Collatz(i, 0);
|
|
if q > record then
|
|
champ:=i; record:=q; fi;
|
|
od;
|
|
g[1]:=champ;
|
|
g[2]:=record;
|
|
.
|