Determine the minimum value of a positive integer N that satisfy the following conditions:
- N is a multiple of 101.
- N starts with and ends in 98.
- sod(N) = 94
*** sod(N) denotes the sum of the digits of N.
For example, sod(3456) = 3+4+5+6 = 18
clearvars
n=[8 9 9 9 9 9 9 9 6 8 9];
pval=1;
for i=2:11
pval(i)=mod(pval(i-1)*10,101);
end
dec=1;
for i=2:11
dec(i)=dec(i-1)*10;
end
totVal=mod(sum(n.*pval),101);
sum(n.*dec)
found=false;
while ~found
for i=4:9
if n(i)<n(i-1)
n(i)=n(i)+1; n(i-1)=n(i-1)-1;
totVal=mod(totVal+pval(i)-pval(i-1),101);
if totVal==0
found=true;
break
end
end
end
end
disp(sum(n.*dec))
fails to find an 11-digit solution. (The procedure was terminated manually as only finding an answer would terminate it by itself, and found to have gotten to the highest value.)
clearvars,clc
n=[8 9 9 9 9 9 9 9 6 0 8 9];
pval=1;
for i=2:12
pval(i)=mod(pval(i-1)*10,101);
end
dec=1;
for i=2:12
dec(i)=dec(i-1)*10;
end
totVal=mod(sum(n.*pval),101);
sum(n.*dec)
found=false;
while ~found
for i=4:10
if n(i)<n(i-1)
n(i)=n(i)+1; n(i-1)=n(i-1)-1;
totVal=mod(totVal+pval(i)-pval(i-1),101);
if totVal==0
found=true;
break
end
end
end
end
disp(sum(n.*dec))
does find a 12-digit solution:
987777888898
The program stores the lowest number with the appropriate beginning, ending and sod. It's stored "backwards", units position first. The positions after the terminal 89 (backward remember) are examined until an decrease in the digit value is found compared to the previous. The previous is decremented and the current one incremented, maintaining the sod. The value of the whole n is adjusted using the modular values of those positions. Once a value mod 101 of zero is found the algorithm stops.
|
Posted by Charlie
on 2023-07-03 13:59:43 |