Question: How to compute a self referencing integer sequence

Requesting a code to compute the following self referencing integer sequence :

a(1)=1, a(2)=0. Thereafter, if a(n) is a novel term then a(n+1)=Min{|a(n)-a(k)|; 1<=k<n}. Otherwise, if a(n) seen before (most recently at a(m)), then a(n+1)=n-m.

First few terms are:

1,0,1,2,1,2,2,1,3,1,2,4,1,3,5,1,3,3,1,3,2,10,5......

Desirable to have option for graphing results up to some value of n.

Thanks in advance for any assistance

David.

 

Please Wait...