On the 30th of the USE, there is absolutely no time for preparation, a program that solves problems of such a plan came across by chance:
The performer has two teams,
which numbers are assigned:
1. add 2
2. multiply by 3.
How many programs are there that the number 1 translates into the number 55?
But it is on Win XP, and I would use it on my iPod. Of course, I can solve the problem, but there you can make a mistake in the calculations + comrade nfiga does not understand this, I would also like to help him. Well, I fired up to write a program for iPod; I don’t have any knowledge of Objective C, I decided to just make a page on the internet in JS, where it will be possible to solve it. But I began to think that I don’t even know how to do something.
The algorithm is as follows: it is gradual to perform this task for numbers less than 55, i.e. first for 2, then for 3, etc. The formula is as follows: R(n) = R(n-2) + R(n/3)
is for this task. R(n)
is the number of programs, n is the number into which to convert. If n is not divisible by 3, then R(n/3)
equated to 0. And it turns out we need to find only R(2)
, R(3)
with our mind and logic, R(2)=0
(since such there are no programs), R(3) = 2
(you can multiply 1 by 3 or add 2 to 1). And then we substitute everything into the first formula: R(4) = R(4-2) + R(4/3)
4/3 is not divisible, therefore it is 0, therefore R(4) = R(2) = 0
. And so on, up to 55. This is the algorithm in principle I imagine how to organize. You can create an array with these values simply and everything and also work, but it will not be R(2)
, in R[2]
. But I do not know how to calculate the first R(2)
, R(3)
using JS.
Help me please. After all, programs are rare, maybe from number 8 to number 27, with different performer operations (for example, they will be multiplied by 2 and added 3). And the question is how to determine the first elements of the array, so that we can build the rest of them from the formula R(n) = R(X) + R(X)
UPD: Here is the program, written thanks to the IVsevolod algorithm:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html><head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1251"> <title>Б13</title> <script type="text/javascript"> function Schet(n) { var f = document.getElementById('forma'); var result = 0; var num = Number(f.num2.value); if(f.can1.value=='plus') var n1 = n + Number(f.k1.value); else if(f.can1.value=='mnozh') var n1 = n * f.k1.value; else if(f.can1.value=='sqr') var n1 = n * n; if(f.can1.value!=""){ if (n1 == num) result++; if (n1 < num) result += Schet(n1); } if(f.can2.value=='plus') var n2 = n + Number(f.k2.value); else if(f.can2.value=='mnozh') var n2 = n * f.k2.value; else if(f.can2.value=='sqr') var n2 = n * n; if(f.can2.value!=""){ if (n2 == num) result++; if (n2 < num) result += Schet(n2); } if(f.can3.value=='plus') var n3 = n + Number(f.k3.value); else if(f.can3.value=='mnozh') var n3 = n * f.k3.value; else if(f.can3.value=='sqr') var n3 = n * n; if(f.can3.value!=""){ if (n3 == num) result++; if (n3 < num) result += Schet(n3); } return result; } function ECHO(msg){ document.getElementById('answer').innerHTML = '<b>Ответ:</b> '+msg; } </script> </head> <body> <form id=forma> <b>Исполнитель умеет выполнять следующие команды:</b><br/> <select name=can1><option value=""> </option><option selected value='plus'>Прибавь</option><option value='mnozh'>Умнож на</option><option value='sqr'>Возведи в квадрат</option></select> <input name=k1 type=text value="2" size=5><br/><br/> <select name=can2><option value=""> </option><option value='plus'>Прибавь</option><option selected value='mnozh'>Умнож на</option><option value='sqr'>Возведи в квадрат</option></select> <input name=k2 type=text value="3" size=5><br/><br/> <select name=can3><option selected value=""> </option><option value='plus'>Прибавь</option><option value='mnozh'>Умнож на</option><option value='sqr'>Возведи в квадрат</option></select> <input name=k3 type=text value="0" size=5><br/><br/> <b>Сколько есть программ, которые преобразуют</b><br/> число <input name=num1 type=text value="1" size=5> в число <input name=num2 type=text value="55" size=5> <br/> <button OnClick="ECHO(Schet(Number(document.getElementById('forma').num1.value)));return false;">Решить!</button> <div id=answer></div> </form> </body> </html>