RosettaCodeData/Task/Hailstone-sequence/Scilab/hailstone-sequence.scilab
2023-07-01 13:44:08 -04:00

33 lines
573 B
Text

function x=hailstone(n)
// iterative definition
// usage: global verbose; verbose=%T; hailstone(27)
global verbose
x=0; loop=%T
while(loop)
x=x+1
if verbose then
printf('%i ',n)
end
if n==1 then
loop=%F
elseif modulo(n,2)==1 then
n=3*n+1
else
n=n/2
end
end
endfunction
global verbose;
verbose=1;
N=hailstone(27);
printf('\n\n%i\n',N);
global verbose;
verbose=0;
N=100000;
M=zeros(N,1);
for k=1:N
M(k)=hailstone(k);
end;
[maxLength,n]=max(M)