Recamán's Sequence: A visual representation

function SEQUENCE = recaman_seq(n_max)
% SEQUENCE = recaman_seq(n_max)
% Generates a Recamán's sequence up to the number n_max.
%
% SEQUENCE : a column vector containing the sequence.
% n_max    : an integrer containing the last number in the sequence.
 
 
% Initializing matrixes and indexes:
NUMBERS = ones(n_max,1,'logical');
SEQUENCE = zeros(n_max,1);
number = 1;
seq_index = 1;
 
% Generating sequence:
while(number <= n_max)
    NUMBERS(number) = false;
    SEQUENCE(seq_index) = number;
     
    seq_index = seq_index + 1;
     
    number_test = number - seq_index;
    if(number_test > 0 && NUMBERS(number_test))
        number = number_test;
    else
        number = number + seq_index;
    end
end
SEQUENCE(seq_index:end) = [];
end

1 comment:

  1. https://www.dropbox.com/s/4l51ls9771zxm4x/recaman_animate.m?dl=0

    ReplyDelete