[Question]
  [
J. E. Maxfield proved following theorem (see [DOI: 10.2307/2688966](http://www.jstor.org/stable/2688966)):
> 
> If \$A\$ is any positive integer having \$m\$ digits, there exists a positive integer \$N\$ such that the first \$m\$ digits of \$N!\$ constitute the integer \$A\$.
> 
> 
> 
### Challenge
Your challenge is given some \$A \geqslant 1\$ find a corresponding \$N \geqslant 1\$.
### Details
* \$N!\$ represents the factorial \$N! = 1\cdot 2 \cdot 3\cdot \ldots \cdot N\$ of \$N\$.
* The digits of \$A\$ in our case are understood to be in base \$10\$.
* Your submission should work for arbitrary \$A\geqslant 1\$ given enough time and memory. Just using e.g. 32-bit types to represent integers is not sufficient.
* You don't necessarily need to output the *least* possible \$N\$.
### Examples
```
A            N
1            1
2            2
3            9
4            8
5            7
6            3
7            6
9           96
12           5
16          89
17          69
18          76
19          63
24           4
72           6
841      12745
206591378  314
```
The least possible \$N\$ for each \$A\$ can be found in <https://oeis.org/A076219>
  
[Answer]
# [Python 2](https://docs.python.org/2/), 50 bytes
```
f=lambda a,n=2,p=1:(`p`.find(a)and f(a,n+1,p*n))+1
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIVEnz9ZIp8DW0EojoSBBLy0zL0UjUTMxL0UhTQMop22oU6CVp6mpbfi/oCgzrwQoqm6orsmF4Jih8MxReBYoPEt1zf8gMQA "Python 2 – 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](https://docs.python.org/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](https://codegolf.meta.stackexchange.com/a/9067/3852) 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:
## [Python 2](https://docs.python.org/2/), 48 bytes
```
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](https://docs.python.org/2/), 47 bytes (invalid)
> 
> 
> 
> ```
> f=lambda a,n=1,p=1:`p`.find(a)and-~f(a,n+1,p*n)
> 
> ```
> 
> [Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIVEnz9ZQp8DW0CqhIEEvLTMvRSNRMzEvRbcuTQMopw2U08rT/F9QlJlXopCmoW5opq7JheCZo/AsUHiW6pr/QWIA "Python 2 – 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`.
> 
> 
> 
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), ~~3~~ 5 bytes
```
ℕ₁ḟa₀
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/pfXa6koGunoFRuX/eobcOjpqaHuzprH26d8P9Ry9RHTY0Pd8xPfNTU8P9/tKGOkY6xjomOqY6ZjrmOpY6hkY6hmY6huY6hhY6hpY6RiY65kY6FCVCZgZmppaGxuUUsAA "Brachylog – Try It Online")
Takes input through its output variable, and outputs through its input variable. (The other way around, it just finds arbitrary prefixes of the input's factorial, which isn't quite as interesting.) Times out on the second-to-last test case on TIO, but [does fine on the last one](https://tio.run/##SypKTM6ozMlPN/r//@GO@YmPmhr@/4/6b2RgZmppaGxuAQA). I've been running it on 841 on my laptop for several minutes at the time of writing this, and it hasn't actually spit out an answer yet, but I have faith in it.
```
         The (implicit) output variable
   a₀    is a prefix of
  ḟ      the factorial of
         the (implicit) input variable
ℕ₁       which is a positive integer.
```
Since the only input `ḟa₀` doesn't work for is 1, and 1 is a positive prefix of 1! = 1, `1|ḟa₀` works just as well.
Also, as of this edit, 841 has been running for nearly three hours and it still hasn't produced an output. I guess computing the factorial of every integer from 1 to 12745 isn't exactly fast.
[Answer]
# [C++ (gcc)](https://gcc.gnu.org/), ~~107~~ 95 bytes, using `-lgmp` and `-lgmpxx`
Thanks to the people in the comments for pointing out some silly mishaps.
```
#import<gmpxx.h>
auto f(auto A){mpz_class n,x=1,z;for(;z!=A;)for(z=x*=++n;z>A;z/=10);return n;}
```
[Try it online!](https://tio.run/##LY7bioMwGISvm6fIsjdm1a122ZO/EXySEuJhhZxIIispffWmans1Mwx8M9yYfOQ8xtdJGm19PUqzLO9/DWKz13hIdmnJRZpw5oI5h1W20DILMGibQHihLZDNBrq80TRVEJoWwpGWBQHb@9kqrOC68hUXc9fjetLO257JBqFJeSzZpBKCL@jgfFdVXM8e1/W6fCq@Pn/Lj@8fsuW97FUnAB2e2ALQNd74INjoYi725w@N@f8d "C++ (gcc) – Try It Online")
Computes \$n!\$ by multiplying \$(n-1)!\$ by \$n\$, then repeatedly divides it by \$10\$ until it is no longer greater than the passed integer. At this point, the loop terminates if the factorial equals the passed integer, or proceeds to the next \$n\$ otherwise.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
1!w⁼1ʋ1#
```
[Try it online!](https://tio.run/##y0rNyan8/99QsfxR4x7DU92Gyv///zc3AgA "Jelly – Try It Online")
Takes an integer and returns a singleton.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
∞.Δ!IÅ?
```
[Try it online](https://tio.run/##yy9OTMpM/f//Ucc8vXNTFD0Pt9r//29kYGZqaWhsbgEA) or [verify -almost- all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWXo/0cd8/TOTVGMONxq/79W53@0oY6RjrGOiY6pjpmOuY6ljqGRjqGZjqG5jqGFjqGljpGJjrmRjpGBmamlobG5RSwA) (`841` times out, so is excluded).
**Explanation:**
```
∞.Δ      # Find the first positive integer which is truthy for:
   !     #  Get the factorial of the current integer
    IÅ?  #  And check if it starts with the input
         # (after which the result is output implicitly)
```
[Answer]
# Pyth - 8 bytes
```
f!x`.!Tz
f              filter. With no second arg, it searches 1.. for first truthy
 !             logical not, here it checks for zero
  x    z       indexof. z is input as string
   `           string repr
    .!T        Factorial of lambda var
```
[Try it online](http://pyth.herokuapp.com/?code=f%21x%60.%21Tz&test_suite=1&test_suite_input=1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9&debug=0).
[Answer]
# JavaScript, ~~47~~ 43 bytes
Output as a BigInt.
```
n=>(g=x=>`${x}`.search(n)?g(x*++i):i)(i=1n)
```
[Try It Online!](https://tio.run/##Rc1LDsIgFADAvafowgXYJ/HRD8WEegiXagLpT5oKTdsYEuPZcelcYEbzNmuz2Hk7Ot92sVfRqZoMKqha7z/hq9namaV5EkcvAwmHNLX0bCmxCh2NjXernzo2@YHcdggcMsihgBIESEAOWAIKwApQAs9BcOCnspCYiWr3YC8zk380m/a6mWUjkqY6UYlOexIoZaO3Tt@dpvEH)
Saved a few bytes by taking Lynn's approach of "building" the factorial rather than calculating it on each iteration so please upvote her solution as well if you're upvoting this one.
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 69 + 22 = 91 bytes
```
a=>{var n=a/a;for(var b=n;!(b+"").StartsWith(a+"");b*=++n);return n;}
```
[Try it online!](https://tio.run/##hY1BS8NAEIXv@RVrT7tGU2tpVdbtoYIgqAg99DxZN3GgnYWZSUFKfns0oHizt/fe9x4vymXMnIZOkFqz@RRN@@q12yfGKL6IOxAxb@ZYiIJiNIeM7@YFkKz7Dn/6jx3F@zW2T6SpTXzxJ1dNGCCsjgdgQwGm4JvMdnR1IH9m63IycdVGgVW2qB8WxsDX56EsyXlO2jEZ8v3gf88eMknepWrLqOkZKdnGzpz7l1@f4PNT@6vl4m42v7kde33RD18 "C# (.NET Core) – Try It Online")
Uses `System.Numerics.BigInteger` which requires a `using` statement.
-1 byte thanks to @ExpiredData!
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 16 bytes
```
‘ɼ!³;D®ß⁼Lḣ@¥¥/?
```
[Try it online!](https://tio.run/##AS8A0P9qZWxsef//4oCYybwhwrM7RMKuw5/igbxM4bijQMKlwqUvP////zIwNjU5MTM3OA "Jelly – Try It Online")
### Explanation
```
‘ɼ                | Increment the register (initially 0)
  !               | Factorial
   ³;             | Prepend the input
     D            | Convert to decimal digits
        ⁼   ¥¥/?  | If the input diguts are equal to...
         Lḣ@      | The same number of diguts from the head of the factorial
      ®           | Return the register
       ß          | Otherwise run the link again
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 23 bytes
```
{+([\*](1..*).../^$_/)}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYPu/WlsjOkYrVsNQT09LU09PTz9OJV5fs/Z/cWKlgkq8jrqCrZ2Cuo4eUG1afpECUJWRgfV/AA "Perl 6 – Try It Online")
### Explanation
```
{                     }  # Anonymous code block
   [\*](1..*)            # From the infinite list of factorials
             ...         # Take up to the first element
                /^$_/    # That starts with the input
 +(                  )   # And return the length of the sequence
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 16 bytes
```
⊞υ¹Ｗ⌕ＩΠυθ⊞υＬυＩ⊟υ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gtDhDo1RHwVDTmqs8IzMnVUHDLTMvRcM5sbhEI6AoP6U0uUSjVFNTR6FQU1MBptonNS@9JAMkbs0VUJSZVwJVn18AEtO0/v/f/L9uWQ4A "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
⊞υ¹
```
Push `1` to the empty list so that it starts off with a defined product.
```
Ｗ⌕ＩΠυθ
```
Repeat while the input cannot be found at the beginning of the product of the list...
```
⊞υＬυ
```
... push the length of the list to itself.
```
Ｉ⊟υ
```
Print the last value pushed to the list.
[Answer]
# [Perl 5](https://www.perl.org/) `-Mbigint -p`, 25 bytes
```
1while($.*=++$\)!~/^$_/}{
```
[Try it online!](https://tio.run/##K0gtyjH9/9@wPCMzJ1VDRU/LVltbJUZTsU4/TiVev7b6/3/Lf/kFJZn5ecX/dX2TMtMz80r@6xYAAA "Perl 5 – Try It Online")
[Answer]
# [J](http://jsoftware.com/), ~~28~~ 22 bytes
*-6 bytes thanks to FrownyFrog*
```
(]+1-0{(E.&":!))^:_&1x
```
[Try it online!](https://tio.run/##bdFNC8IwDAbge3/Fqwd1qGPZunUreFH0JB68q6BM/GSgEwV//NRGEYOB9vBQkpd0V9X95ho9iyY6CGCfp@tjMB2PqtasTd3g3hr6jbqted7cLhp0qzwFVypfbQoQelg/70@xhk5DoZnTSGjqVAs1TmOhkdNEaOLUyGnMmeCYA4ciBGejRHR@sxHhuDWl4jXHo@yXNS9D/w39zTHp@7huDwdci9Mey0uJcnvMzygupQXvOjSa46eafvdC7yFBEmcUmRTuk16XUqp6AA "J – Try It Online")
# original answer [J](http://jsoftware.com/), 28 bytes
```
>:@]^:(-.@{.@E.&":!)^:_ x:@1
```
[Try it online!](https://tio.run/##bdHLCsIwEAXQfb7i6sIHaDBt2rQBpSi6EhfuraC0@KSgFQU/vmpGEQcHksUhzFwmu6oumzn6Fk100IN9nq7EaD6dVAObLFLb6srkLpOxbNRtrZ3aJW42UVVbwJXI1psCCn3kz/tTpJ5Tj2ns1GcaOdVMjdOAqe80ZBo6NXwaccw4oMAeC0HZVMg6v9mwcNRaRew1xVPxL2tahv4b@ptjNpS4bg8HXIvTHqtLiXJ7zM4oLqUF7dozmuJHWv3uRb2H9MIgVr6J4D7pdQkhqgc "J – Try It Online")
* `>:@] ... x:@1` starting with an extended precision `1`, keep incrementing it while...
* `-.@` its not the case that...
* `{.@` the first elm is a starting match of...
* `E.&":` all the substring matches (after stringfying both arguments `&":`) of searching for the original input in...
* `!` the factorial of the number we're incrementing
[Answer]
# [C (gcc)](https://gcc.gnu.org/) -lgmp, 161 bytes
```
#include"gmp.h"
f(a,n,_,b)char*a,*b;mpz_t n,_;{for(mpz_init_set_si(n,1),mpz_init_set(_,n);b=mpz_get_str(0,10,_),strstr(b,a)-b;mpz_add_ui(n,n,1),mpz_mul(_,_,n));}
```
[Try it online!](https://tio.run/##VZDRasQgEEXf8xVDSkHDpGzoo/RL2kWMia5g3GBMH7rsr9fOdJfSFYQ7d2YOV23vra31KSQb92lu/bK@nNrGCYMJNY7SnkzuDHajWtYvXYBcdXHnLLgMKRS9zXSDSDhI/G8KjUmq8Y09zzMliwMOB9QSSXM5opH9jWymSe9M@eMseyQEQ6S61pAKLCYkwcJkbxE4GnSkP9@PEi4N0LmFzPO2x4LgjC2q@W3Qi3hwOOJDV97bayauE@3z9pFahMfMQKHvW5Sludb6@m1dNH6rfaQf@wE "C (gcc) – Try It Online")
[Answer]
# [Python 3](https://docs.python.org/3/), 63 bytes
```
f=lambda x,a=2,b=1:str(b).find(str(x))==0and a-1or f(x,a+1,b*a)
```
[Try it online!](https://tio.run/##FYpBCoQwDAC/0mOiXbGKrgp5TEopFnZjqRX09VXnMMxh4pXXTfpSPP34bx2rUzN12pJZ9pzAYuODOHj7RCRqWZzij9mS8vC8tdG2YiwxBcng4XWQeGTAh9K14zCb/jvd "Python 3 – Try It Online")
-24 bytes thanks to Jo King
-3 bytes thanks to Chas Brown
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 11 bytes
```
‘ɼµ®!Dw³’µ¿
```
[Try it online!](https://tio.run/##y0rNyan8//9Rw4yTew5tPbRO0aX80OZHDTOB7P3///@PNtIx0DHTMdWx1DHUMdYx17GIBQA "Jelly – Try It Online")
[Answer]
# [Clean](https://github.com/Ourous/curated-clean-linux), 88 bytes
```
import StdEnv,Data.Integer,Text
$a=hd[n\\n<-[a/a..]|startsWith(""<+a)(""<+prod[one..n])]
```
[Try it online!](https://tio.run/##LY3NbsIwEITvfYot4pCIYOIUCFThRg9IPVSiUg8hhy0xYGGvI2cDReqz101/Dp9GMxrN7I1CCtbVnVFgUVPQtnGeYcv1E12SNTKKDbE6Kp@8qg@@G@LqVJe021ExLnGCQlSfLaPn9k3zKRoMihHGv9J4V5eOlBBUxVXY/pRg1b80ELHbstd0BAfDHnb/HzFEpRRiWcFoBKXMEpDznrxn0bNMIJsmkPd5ls5nS/mQL6r4bjKBqzYG1EURd2jMDa7On@HgPCymMoH3jkEzMJ5VC21nI7p/lAUVMsunsxhcozyydtSGr/3B4LEN481zWN8Ird7/mReD3O/ZMD4Fmaap/QY "Clean – Try It Online")
Defines `$ :: Integer -> Integer`.
Uses `Data.Integer`'s arbitrary size integers for IO.
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 62 bytes
```
(b=1;While[⌊b!/10^((i=IntegerLength)[b!]-i@#)⌋!=#,b++];b)&
```
[Try it online!](https://tio.run/##DcrBCoIwHAfgV0mEUPzF/M/pFBl4DTp06yAGLsYcpAfZLXqB8il9kdV3/ubRT2YevXuMwaqQaEXtbXJP0@/bR0eM8nuSOHVevLFmvZjF@intdTScXBen@/aNVAydZUOr02O4rm7xB9ZZ1r0IHAUESlSQaEAcVIEkqAY14AKSoxb/lldlQ4Ws3yH8AA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), 40 bytes
```
->n{a=b=1;a*=b+=1until"%d"%a=~/^#{n}/;b}
```
[Try it online!](https://tio.run/##KypNqvyfZvtf1y6vOtE2ydbQOlHLNknb1rA0ryQzR0k1RUk10bZOP065Oq9W3zqp9r@GoZ6ekYGmXm5iQXVNRY1CQWlJsQJQnYKunQJIdXSFTlp0RWxs7X91Qwt1AA "Ruby – Try It Online")
[Answer]
# [Icon](https://github.com/gtownsend/icon), ~~65~~ 63 bytes
```
procedure f(a);p:=1;every n:=seq()&1=find(a,p*:=n)&return n;end
```
[Try it online!](https://tio.run/##TY3NDoIwEAbvPMXKgbTmM3HLf0l9EeOBQEk4uGAFjU@Pmnhw7jMzdpNs2xymzvdr8DSoVjezddz4hw8vEuvu/qZ0wm4YpVct5r11opPglzUISeOl//Ov7ShKR/ThFyDraHdmGKTIkKNAiRpswAW4BFfgGiZDaWCORV5zWlYX6id6hnHxShAfThRjUKJ19J29AQ "Icon – Try It Online")
[Answer]
# Haskell, 89 bytes
```
import Data.List
a x=head$filter(isPrefixOf$show x)$((show.product.(\x->[1..x]))<$>[1..])
```
If anyone knows how to bypass the required import, let me know.
]