f=lambda a,n=2,p=1:(`p`.find(a)and f(a,n+1,p*n))+1
Try it online!
This is a variation of the 47-byte solution explained below, adjusted to return 1
for input '1'
. (Namely, we add 1
to the full expression rather than the recursive call, and start counting from n==2
to remove one layer of depth, balancing the result out for all non-'1'
inputs.)
Python 2, 45 bytes (maps 1 to True
)
f=lambda a,n=2,p=1:`-a`in`-p`or-~f(a,n+1,p*n)
This is another variation, by @Jo King and @xnor, which takes input as a number and returns True
for input 1
. Some people think this is fair game, but I personally find it a little weird.
But it costs only 3 bytes to wrap the icky Boolean result in +()
, giving us a shorter "nice" solution:
f=lambda a,n=2,p=1:+(`-a`in`-p`)or-~f(a,n+1,p*n)
This is my previous solution, which returns 0
for input '1'
. It would have been valid if the question concerned a non-negative N
.
Python 2, 47 bytes (invalid)
f=lambda a,n=1,p=1:`p`.find(a)and-~f(a,n+1,p*n)
Try it online!
Takes a string as input, like f('18')
.
The trick here is that x.find(y) == 0
precisely when x.startswith(y)
.
The and
-expression will short circuit at `p`.find(a)
with result 0
as soon as `p`
starts with a
; otherwise, it will evaluate to -~f(a,n+1,p*n)
, id est 1 + f(a,n+1,p*n)
.
The end result is 1 + (1 + (1 + (... + 0)))
, n
layers deep, so n
.