10
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ It looks like it could be a dupe, sorry if it is - didn't find a similar question. \$\endgroup\$ – nicael Jun 21 '16 at 21:11
  • 4
    \$\begingroup\$ What 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. \$\endgroup\$ – Peter Taylor Jun 21 '16 at 22:23
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Deusovi Jun 22 '16 at 4:22
  • \$\begingroup\$ @Peter Fair remark, restricted to positive numbers. \$\endgroup\$ – nicael Jun 22 '16 at 6:39
1
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ Timed out, with such small numbers? Huh. \$\endgroup\$ – nicael Jun 21 '16 at 21:54
  • \$\begingroup\$ @nicael yeah, there's only 59K ways that I check for 10, but pyth is slooooooow \$\endgroup\$ – Maltysen Jun 21 '16 at 21:56
6
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Interesting 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. \$\endgroup\$ – Patrick Roberts Jun 22 '16 at 3:55
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.