Данный код делает даже больше - подсчитывает сколькими способами можно перевести начальное число в конечное, и как именно. Формат вывода подогнать не сложно
# Код на ruby 2.2.3p173
def f0(number, log) #
n = 10 * number + 1
log += " + 1 "
log += "[" + n.to_s + "] "
return [n, log]
end
def f1(number, log) #
n = 2 * number
log += " * 2 "
log += "[" + n.to_s + "] "
return [n, log]
end
def f2(number, log) #
n = number + 4
log += " + 4 "
log += "[" + n.to_s + "] "
return [n, log]
end
start_num = 14 # начальное число
end_num = 28 # конечное число
max_steps = end_num
op_numbers = 2
count = 0
for steps in 1..max_steps
for i in 0..op_numbers**steps-1
s = ("000000000000000000000000000000" + i.to_s(op_numbers))[-steps..-1]
num = start_num
log = start_num.to_s
for k in 0..s.size - 1
num, log = f0(num, log) if s[k] == "0"
num, log = f1(num, log) if s[k] == "1"
# num, log = f2(num, log) if s[k] == "2"
break if num > end_num
end
# p [num, log, s, steps, i]
if num == end_num
log += " = " + end_num.to_s
count += 1
p log
end
end
end
p count