% Matlab script for creating sequence A316588 on The On-Line Encyclopedia of Integer Sequences % By Daniƫl Karssen 2018 %% Board numbering % 1 2 4 7 11 16 % 3 5 8 12 17 % 6 9 13 18 % 10 14 19 % 15 20 % 21 %% Creating the sequence x0 = 1; y0 = 1; seqNum = 1; seqX = x0; seqY = y0; while true % apply all eight possible moves dx = [ 2 2 -2 -2 1 1 -1 -1]; dy = [ 1 -1 1 -1 2 -2 2 -2]; x = x0 + dx; y = y0 + dy; % determine number of possible squares to move to num = (x+y-2).*(x+y-1)/2 + y; % select all unvisited squares to are on the board sel = ~ismember(num,seqNum) & x>0 & y>0; % break if no potential squares are available if (~any(sel)) disp('STOPPED') break end % select available square with lowest number as next square num(~sel) = inf; [seqNum(end+1), index] = min(num); x0 = x(index); y0 = y(index); seqX(end+1) = x0; seqY(end+1) = y0; end %% Figure xy2num = @(x,y) (x+y-2).*(x+y-1)/2 + y; hFig = figure; hold on plot3(1,1,1,'ro') plot3(seqX(end),seqY(end),numel(seqX),'rx') patch([seqX nan],[seqY nan],[1:numel(seqX) nan],[1:numel(seqX) nan],'EdgeColor','interp','FaceColor','none') set(gca,'Ydir', 'reverse', 'XAxisLocation', 'Top') legend('n=1',sprintf('n=%i',numel(seqX)),'Location','southeast') axis equal xlim([0 max(seqX)+1]) ylim([0 max(seqY)+1]) saveas(hFig,'a316588.svg') %% String of start of sequence seqString = sprintf('%i, ',seqNum); maxChr = 260; i = find(seqString==',' & 1:numel(seqString)<=maxChr,1,'last'); seqString = seqString(1:i-1) %% Create b-file fid = fopen('b316588.txt','w'); fprintf(fid,'%i %i\r\n',[1:numel(seqNum); seqNum]); fclose(fid);