
function flag=numero_felice(n,maxit)

flag=0;

if (n < 0) | (n >= 1000)
    fprintf('\n \t Numero non accettabile');
    flag=2; return;
end

for k=1:maxit
    c1=floor(n/100);
    resto=n-c1*100;
    c2=floor(resto/10);
    c3=resto-c2*10;
    

    m=c1^2+c2^2+c3^2;
    if m == 1
        flag=1;
        return;
    end

    n=m;

end

