How can you go from the number 11 to 25 by only multiplying by 2 or decreasing by 3 in a minimum number of steps?
clearvars,clc
global path shortest shortpath t shortt
shortest=30;
path=[11]; t='';
addon
shortest
shortpath
shortt
function addon()
global path shortest shortpath t shortt
for type = 1:2
savepath=path;
savet=t;
if type==2
path(end+1)=path(end)-3;
t(end+1)='-';
else
path(end+1)=path(end)*2;
t(end+1)='*';
end
if path(end)==25
if length(path)<shortest
shortest=length(path);
shortpath=path;
shortt=t;
end
else
if length(path)<10
addon;
end
end
path=savepath;
t=savet;
end
end
shows
shortest =
8
shortpath =
11 8 5 10 7 14 28 25
shortt =
'--*-**-'
indicating the sequence is 7 steps long: 2 subtractions, 1 multiplication, 1 subtraction, 2 multiplications, and 1 final subtraction, making the sequence of numbers:
11, 8, 5, 10, 7, 14, 28, 25
|
Posted by Charlie
on 2025-05-14 08:33:20 |