For n in {1,2,...,9}, find all n-digit positive integers, which are
(1) n-pandigital, i.e. formed from a permutation of the digits 1 to n, with no repeat digits; and
(2) k-divisible, i.e. for all k, k ≤ n, the integer formed from the truncated left most k digits is evenly divisible by k.
And for n=10, also find all 10-digit pandigitals with the same second condition.
Example: 2136547 almost qualifies, but fails for k=2.
2136547 is divisible by 7
213654 is divisible by 6
21365 is divisible by 5
2136 is divisible by 4
213 is divisible by 3
21 is not divisible by 2
2 is divisible by 1
clearvars,clc
global sols s
p=perms('1':'9'); sols=[];
for i=1:length(p)
s=p(i,:);
for l=1:9
check(s(1:l))
end
end
fprintf('%10d\n',sort(sols));
function check(ns)
global sols s
tst=sort(ns);
if str2double(tst(length(ns)))==length(ns)
good=true;
for l=1:length(ns)
if mod(str2double(ns(1:l)),l)~=0
good=false;
break
end
end
if good
if ~ismember(str2double(ns),sols)
sols(end+1)=str2double(ns);
end
end
end
end
finds
1
12
123
321
123654
321654
38165472
381654729
A 10-digit pandigital that's divisible by 10 must end in zero. It must therefore be the 9-digit pandigital with a zero appended: 3816547290.
|
Posted by Charlie
on 2023-10-08 12:58:55 |