Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

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

No comments

Popular Posts

Followers