function pyrammath
% à optimiser
global triangle paspris
trouve=0;

l15=1:15;
while ~trouve
    for nbdepart=15:-1:1;
        triangle=zeros(5,5);
        paspris=ones(1,15);
        paspris(nbdepart)=0;
        triangle(1,1)=nbdepart;
        liste=l15(find(paspris));
        nb=fix(rand*length(liste))+1;
        triangle(1,2)=liste(nb);
        triangle(2,1)=abs(triangle(1,1)-triangle(1,2));
        ok=test(2);
        if ok
            paspris(triangle(1,2))=0;
            paspris(triangle(2,1))=0;
            liste=l15(find(paspris));
            nb=fix(rand*length(liste))+1;
            triangle(1,3)=liste(nb);
            triangle(2,2)=abs(triangle(1,2)-triangle(1,3));
            triangle(3,1)=abs(triangle(2,1)-triangle(2,2));
            ok=test(3);
            if ok
                paspris(triangle(1,3))=0;
                paspris(triangle(2,2))=0;
                paspris(triangle(3,1))=0;
                liste=l15(find(paspris));
                nb=fix(rand*length(liste))+1;
                triangle(1,4)=liste(nb);
                triangle(2,3)=abs(triangle(1,3)-triangle(1,4));
                triangle(3,2)=abs(triangle(2,2)-triangle(2,3));
                triangle(4,1)=abs(triangle(3,1)-triangle(3,2));
                ok=test(4);
                if ok
                    paspris(triangle(1,4))=0;
                    paspris(triangle(2,3))=0;
                    paspris(triangle(3,2))=0;
                    paspris(triangle(4,1))=0;
                    liste=l15(find(paspris));
                    nb=fix(rand*length(liste))+1;
                    triangle(1,5)=liste(nb);
                    triangle(2,4)=abs(triangle(1,4)-triangle(1,5));
                    triangle(3,3)=abs(triangle(2,3)-triangle(2,4));
                    triangle(4,2)=abs(triangle(3,2)-triangle(3,3));
                    triangle(5,1)=abs(triangle(4,1)-triangle(4,2));
                    ok=test(5);
                    if ok, 
                        trouve=1;
                        triangle
                    end
                end
            end
        end
    end
end

function ok=test(dim)
global triangle paspris
% on applatit
mat=[];
for i=1:dim
    mat=[mat triangle(i,1:dim-i+1)];
end
% on trie
mat=sort(mat);
% on teste une paire
n=length(mat);
if ~mat(1)
    ok=0;
elseif sum(mat(1:n-1)==mat(2:n))>0
    ok=0;
else
    ok=1;
end
% vérif
% triangle
% ok
% pause

% solution:

%     13     3    15    14     6
%     10    12     1     8     0
%      2    11     7     0     0
%      9     4     0     0     0
%      5     0     0     0     0
