# Count the number of shortest paths to n

## JavaScript (ES6),  111 ... 84  80 bytes

Returns true rather than $$\1\$$ for $$\n=2\$$.

f=(n,j)=>(g=(i,x,e=1)=>i?e>n?g(i-1,x):g(i-1,x+e)+g(i,x,e*x):x==n)(j,2)||f(n,-~j)


Try it online!

### Commented

f = (                     // f is the main recursive function taking:
n,                      //   n = input
j                       //   j = maximum number of steps
) => (                    //
g = (                   // g is another recursive function taking:
i,                    //   i = number of remaining steps
x,                    //   x = current sum
e = 1                 //   e = current exponentiated part
) =>                    //
i ?                   // if there's still at least one step to go:
e > n ?             //   if e is greater than n:
//     add the result of a recursive call with:
g(i - 1, x)       //       i - 1, x unchanged and e = 1
:                   //   else:
//     add the sum of recursive calls with:
g(i - 1, x + e) + //       i - 1, x + e and e = 1
g(i, x, e * x)    //       i unchanged, x unchanged and e = e * x
:                     // else:
x == n              //   stop recursion; return 1 if x = n
)(j, 2)                   // initial call to g with i = j and x = 2
|| f(n, -~j)              // if it fails, try again with j + 1


This implementation uses a breath-first-search in the "tree" of iteratively all necessary mappings x -> x + x^j.

j#x=x+x^j
f n=[sum[1|x<-l,x==n]|l<-iterate((#)<$>[0..n]<*>)[2],neleml]!!0  Try it online! ### Explanation -- computes the mapping x -> x + x^j j#x=x+x^j --iteratively apply this function for all exponents [0,1,...,n] (for all previous values, starting with the only value [2]) iterate((#)<$>[0..n]<*>)[2]
-- find each iteration where our target number occurs
[                   |l<-...........................,neleml]
-- find how many times it occurs
sum   [1|x<-l,x==n]
-- pick the first entry
f n=.............................................................!!0


## 05AB1E, 17 bytes

2¸[˜DIå#εIÝmy+]I¢


Try it online!