September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,19 +1,11 @@
function x = hailstone(n)
% iterative definition
global VERBOSE;
x = 1;
while (1)
if VERBOSE,
printf('%i ',n); % print element
end;
if n==1,
return;
elseif mod(n,2),
n = 3*n+1;
else
n = n/2;
end;
x = x + 1;
end;
end;
function x = hailstone(n)
x = n;
while n > 1
% faster than mod(n, 2)
if n ~= floor(n / 2) * 2
n = n * 3 + 1;
else
n = n / 2;
end
x(end + 1) = n; %#ok
end

View file

@ -1,4 +1,2 @@
global VERBOSE;
VERBOSE = 1; % display of sequence elements turned on
N = hailstone(27); %display sequence
printf('\n\n%i\n',N); %
x = hailstone(27);
fprintf('hailstone(27): %d %d %d %d ... %d %d %d %d\nnumber of elements: %d\n', x(1:4), x(end-3:end), numel(x))

View file

@ -1,8 +1,9 @@
global VERBOSE;
VERBOSE = 0; % display of sequence elements turned off
N = 100000;
M = zeros(N,1);
for k=1:N,
M(k) = hailstone(k); %display sequence
end;
[maxLength, n] = max(M)
N = 1e5;
maxLen = 0;
for k = 1:N
kLen = numel(hailstone(k));
if kLen > maxLen
maxLen = kLen;
n = k;
end
end

View file

@ -0,0 +1,18 @@
function [n, maxLen] = longestHailstone(N)
maxLen = 0;
for k = 1:N
a = k;
kLen = 1;
while a > 1
if a ~= floor(a / 2) * 2
a = a * 3 + 1;
else
a = a / 2;
end
kLen = kLen + 1;
end
if kLen > maxLen
maxLen = kLen;
n = k;
end
end

View file

@ -0,0 +1,3 @@
>> [n, maxLen] = longestHailstone(1e5)
n = 77031
maxLen = 351

View file

@ -0,0 +1,22 @@
function [n, maxLen] = longestHailstone(N)
lenList(N) = 0;
lenList(1) = 1;
maxLen = 0;
for k = 2:N
a = k;
kLen = 0;
while a >= k
if a == floor(a / 2) * 2
a = a / 2;
else
a = a * 3 + 1;
end
kLen = kLen + 1;
end
kLen = kLen + lenList(a);
lenList(k) = kLen;
if kLen > maxLen
maxLen = kLen;
n = k;
end
end

View file

@ -0,0 +1,3 @@
>> [n, maxLen] = longestHailstone(1e5)
n = 77031
maxLen = 351