Ways to get to the number

10

Given the input of the first number and the second number (both positive integers, zero exluded), determine in how many ways could you make the second out of the first, using following actions: +1,+2 and *3. Operations are simply applied from left to right.

Examples:

  1. Input: 1 2. Output: 1. I.e, you could only get 2 by doing +1, so one way.

  2. Input: 1 3. Output: 3. I.e, you could get 3 by either doing +2 or +1+1, or *3

  3. Input: 1 4. Output: 4.

  4. Input: 2 6. Output: 6.

  5. Input: 2 7. Output: 9.

  6. Input: 1 10. Output: 84.

In case there're no ways, e.g. 100 100, or 100 80, output is 0.

You could also take input as an array, or string with any convenient separator.

The shortest solution wins.

nicael

Posted 2016-06-21T21:10:44.150

Reputation: 4 585

It looks like it could be a dupe, sorry if it is - didn't find a similar question. – nicael – 2016-06-21T21:11:51.743

4What about inputs for which the answer should be infinite? E.g. any input where the first integer is negative, because you can multiply by three and then increment back to the original number and repeat as many times as you want. – Peter Taylor – 2016-06-21T22:23:02.040

1@Patrick: It does make sense though. Starting from -1 and going to 0, you can apply *3 +2 +1 as many times as you want, then apply +1 to get to 0. – Deusovi – 2016-06-22T04:22:23.670

@Peter Fair remark, restricted to positive numbers. – nicael – 2016-06-22T06:39:43.900

Answers

1

Pyth - 26 24 bytes

There seems to be a bug in Pyth that's making it take inputs in the wrong order, but it shouldn't matter anyway.

/m.vj;+sdzs^Lc3"+1+2*3"S

Test Suite.

(1 10 timed out online, but worked on my computer).

Maltysen

Posted 2016-06-21T21:10:44.150

Reputation: 25 023

Timed out, with such small numbers? Huh. – nicael – 2016-06-21T21:54:38.680

@nicael yeah, there's only 59K ways that I check for 10, but pyth is slooooooow – Maltysen – 2016-06-21T21:56:35.987

6

Javascript ES6, 45 44 bytes

f=(a,b=B)=>a<(B=b)?f(a+1)+f(a+2)+f(a*3):a==b

Example runs:

f(1,2)  -> 1
f(2,6)  -> 6
f(1 10) -> 84

Dendrobium

Posted 2016-06-21T21:10:44.150

Reputation: 2 412

1Interesting use of default parameters, though admittedly it doesn't save any bytes here. =B and (B=) (b omitted on purpose) is 6 characters and the alternative is passing ,b 3 times to the recursive calls which is also 6 characters. Anyway, good job. – Patrick Roberts – 2016-06-22T03:55:45.110

1

Pyth, 29 bytes

M?>GH0|qGH+sgLGtBtH?%H3ZgG/H3

Try it online!

Defines a function. Added three bytes in the link to call the function.

Leaky Nun

Posted 2016-06-21T21:10:44.150

Reputation: 45 011