Visualizing the Recamán sequence

A quick attempt at a visualization of the Recamán sequence similar to that seen on a Numberphile video : https://www.youtube.com/watch?v=FGC5TdIiT9U

More about the sequence : http://mathworld.wolfram.com/RecamansSequence.html

The Matlab code : 

function [  ] = recaman(N)

Sample=ones(1,N);
for i=2:N
    if Sample(i-1)-i > 0 && max(ismember(Sample,(Sample(i-1)-i)))==0
        Sample(i)=Sample(i-1)-i;
    else
        Sample(i)=Sample(i-1)+i;
    end
end

for i=1:length(Sample)-1
    arc(Sample(i),Sample(i+1));
end

    function [] = arc(b, e)
        
        r = abs((e-b)/2);
        ang = 0:0.01:pi;
        xp = r*cos(ang);
        yp = r*sin(ang);
        mid = r+b;
        
        if e < b
            yp = -yp;
            mid = r+e;
        end
        
        plot(mid+xp,yp);
        
        axis equal
        hold on
        
    end

end


Commentaires