clear all
time=cputime;
n=10000000
no_mx = 1+(length(int2str(n))-1)*9^2;
for i=1:no_mx
    sq = i;
    for z=1:1000
        no = length(int2str(sq));
        new = 0;
     for j=1:no
        d = rem(sq,10);
        sq = floor(sq/10);
        new = new + d^2;
     end
        sq = new;
        if (sq == 89)
            sqs(i)=89;
           break
        elseif (sq == 1)
            sqs(i)=1;
            break
        end
    end
end

ctr89=0;
for k=1:n
    no = length(int2str(k));
        new = 0;
        sq=k;
     for j=1:no
        d = rem(sq,10);
        sq = floor(sq/10);
        new = new + d^2;
     end
     val = sqs(new);
     if(val==89)
         ctr89=ctr89+1;
     end
end
disp('elapsed time\n')
ttiime = cputime - time
disp('89 numbers\n')
ctr89
n =

    10000000

elapsed time\n

ttiime =

  517.3906

89 numbers\n

ctr89 =

     8581146