Of the 6-digit palindromes composed of 3 distinct digits which can be formed by adding together three 5-digit palindromes:
a) which is the largest?
b) which is closest to a perfect square?
c) what is the largest palindrome you can form by adding some of these 6-digit
palindromes together (using at most one copy of each 6-digit palindrome)?
d) same question as (c) but duplicate addends allowed?
pals5=[]; ct=0; pSquareDist=999999; largest=0;
for h=100:999
d1=floor(h/100); d2=mod(floor(h/10),10);
pal5=100*h+10*d2+d1;
pals5=[pals5 pal5];
end
for a=1:900
disp(a)
for b=a:900
for c=b:900
p6=sum(pals5([a b c]));
if p6>99999
if ispal(p6)
p6s=char(string(p6));
if length(unique(p6s))==3
if p6>largest
largest=p6;
lgabc=[a b c];
end
sr=sqrt(p6); srRound=round(sr);
sq=srRound^2;
if abs(sq-p6)<pSquareDist
pSquareDist=abs(sq-p6);
nearSquare=p6;
end
ct=ct+1;
end
end
end
end
end
end
disp(ct)
disp(largest)
disp([pals5(lgabc)])
disp(nearSquare)
function ip=ispal(x)
s=char(string(x));
good=true;
for i=1:floor(length(s)/2)
if s(i)~=s(length(s)+1-i)
good=false;
break
end
end
if good
ip=true;
else
ip=false;
end
end
finds
19743 such six digit palindromes
243342 the largest , consisting of 44044 + 99299 + 99999
123321 the nearest to a perfect square (351^2 = 123201)
|
Posted by Charlie
on 2022-04-20 12:50:54 |