% ans =
% 296962999629
function result = euler49()
tic;
result = '';
for i=1488:9999
% get current string
a = num2str(i);
% except 1487, 4817, 8147
if strcmp(unique(a),'1478')
continue;
end
% all permutation of current number's digits
b = perms(a);
% only check primes
b(~isprime(str2num(b)),:) = [];
% less than 3 left, jumpt over
if size(b,1)<3
continue;
end
% sort ascend
b = sortrows(b);
% 2nd-1st==3rd-2nd, so 1st+3rd=2*2nd
% b(j), 1st
for j=1:size(b,1)
% b(k), 3rd
for k=size(b,1):-1:j+1
% mid, 2nd
mid = (str2double(b(j,:))+str2double(b(k,:)))/2;
% b(j,:)!=b(k,:) and 2nd is a row of b
if ~strcmp(b(j,:),b(k,:)) && ismember(num2str(mid),b,'rows')
result = [b(j,:),num2str(mid),b(k,:)];
break
end
end
% find, break
if ~isempty(result)
break;
end
end
% find, break
if ~isempty(result)
break;
end
end % end of i
toc;
end