[Question]
  [
Consider a square grid on the plane, with unit spacing. A line segment of integer length \$L\$ is dropped at an arbitrary position with arbitrary orientation. The segment is said to "touch" a square if it intersects the interior of the square (not just its border).
# The challenge
What is the maximum number of squares that the segment can touch, as a function of \$L\$?
# Examples
* **L=3** \$\ \, \$ The answer is \$7\$, as illustrated by the blue segment in the left-hand side image (click for a larger view). The red and yellow segments only touch \$6\$ and \$4\$ squares respectively. The purple segment touches \$0\$ squares (only the interiors count).
* **L=5** \$\ \, \$ The answer is \$9\$. The dark red segment in the right-hand side image touches \$6\$ squares (note that \$5^2 = 3^2+4^2\$), whereas the green one touches \$8\$. The light blue segment touches \$9\$ squares, which is the maximum for this \$L\$.
[![enter image description here](https://i.stack.imgur.com/pjZfr.png)](https://i.stack.imgur.com/pjZfr.png)
# Additional rules
* The input \$L\$ is a positive integer.
* The algorithm should theoretically work for arbitrarily large \$L\$. In practice it is acceptable if the program is limited by time, memory, or data-type size.
* Input and output are [flexible](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods) as usual. [Programs or functions](http://meta.codegolf.stackexchange.com/a/2422/36398) are allowed, in any [programming language](https://codegolf.meta.stackexchange.com/questions/2028/what-are-programming-languages/2073#2073). [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default) are forbidden.
* Shortest code in bytes wins.
# Test cases
Here are the outputs for `L = 1, 2, ..., 50` (with `L` increasing left to right, then down):
```
 3    5    7    8    9   11   12   14   15   17
18   19   21   22   24   25   27   28   29   31
32   34   35   36   38   39   41   42   43   45
46   48   49   51   52   53   55   56   58   59 
60   62   63   65   66   68   69   70   72   73
```
  
[Answer]
# [Python 2](https://docs.python.org/2/), 27 bytes
```
lambda L:(2*L*L-2)**.5//1+3
```
[Try it online!](https://tio.run/##TZhBjl23EUXH0Sr@sFvuxKwiq1g0oB30DhwPFESyDSiyoWiS1St1LhUggyYg1AX0/@Hl@zzvz/98/e2Pz/7t47u/f/v0/l//@Of7x@tPT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lbSWnK2FfOlJSkjFJSmjJKSdnKkKmMUlKuMo6SUpYhXRlHSQnLONf4rvLJ@bRTVwyvGV41vG5o2qn/2aGS1w@vIGqnviuidupK4rXEq4nXE007dU3xquJ1xSuLpp26unh98QrjNcarjNcZTTt1rfFq4/XGK46mnbrqeN3xyuO1R9NOXX@8AnkN8irkdcgrkaaduhp5PfKK5DVJ005dl7wyeW3y6uT1ySuUpp26Snmd8krltUrTTl2vvGJ5zfKq5XVLyaXJLk16afJLk2CaDLNX@bdOkyTTZJkmzexVnq7TJNM0qab51XmdJr9Cf43@Kv11ep0mKWev1/yV1E5JO03eaRJPk3ma1NPknib5NNmnST971bsEnSYZqElBTQ7aq76XRF8aavJQk4iaTNSkor0qqdMkGzXpqMlHe9X30mmSkpqc1CSlJis1aWmvSuo0yUxNampy0171vXSapKcmPzUJqslQTYpqclSTpJos1aSpJk/tNbXelyRK6jRJVnvV2xKdJvmqSVhNxmpSVpOz9qqkTpO01eZ98aKdmvf9i07TvG9g7tsX7ZT01eSvJoE1GaxJYU0Oa5LYXvW6RqdJHmsSWZPJ9npf6yipnZLNmnTW5LMmoe1VSZ0mOa1Jak1W26u@l06TxNZktia1NbmtSW57VVKnSX5rElyT4faq76XTJMk1Wa5Jc3vV99JOyXRNqmtyXZPsmmy3V72G0mmS8JqM16S8vertoU6TrLfX/cubN/cN5M8fnz49P3hJ@en/X1I@7PHD49OHz0/fX0w@P//yePfu8f1f3/4L "Python 2 – Try It Online")
A direct formula:
$$ f(L) = \lfloor \sqrt{2 L^2-2}\rfloor + 3 $$
**Derivation**
As noted [by @Kirill L.](https://codegolf.stackexchange.com/a/231179/20260) and others, the optimal layout uses a near-diagonal line segment whose horizontal and vertical span are at least \$(h,h)\$ or \$(h,h+1)\$. We need the length-\$L\$ to cover at least this much distance using the Pythagorean theorem, plus some extra to reach into squares and hit 3 more.
This gives a result of either:
* \$2h+3\$ where \$h\$ is the greatest positive integer where \$h^2+h^2 < L^2\$, or
* \$2h+4\$ where \$h\$ is the greatest positive integer where \$h^2+(h+1)^2 <L^2\$,
whichever of these is larger. We want to combine these two cases into one.
Let's start by making the two cases look more similar. Note that the second case can be rewritten as
> 
> \$2(h+\frac{1}{2})+3\$, where \$2(h+\frac{1}{2})^2 + \frac{1}{2} < L^2\$
> 
> 
> 
or as
> 
> \$2h+3\$, where \$2h^2 + \frac{1}{2} < L^2\$
> 
> 
> 
where \$h\$ is a positve integer-and-a-half.
The \$2h^2<L^2\$ in the first case can be also be written as \$2h^2 +\frac{1}{2} < L^2\$, which is equivalent because both sides were integers. So, the cases now merge into:
> 
> \$2h+3\$ where \$h\$ is the greatest positive integer or half-integer
> where \$2h^2 +1/2 < L^2\$
> 
> 
> 
Calling \$2h=H\$, this is:
> 
> \$H+3\$ where \$H\$ is the greatest positive integer
> where \$2(H/2)^2 +1/2 < L^2\$.
> 
> 
> 
This inequality is \$H^2 < 2L^2-1\$, and since these are integers, this is the same as \$H^2 \leq 2L^2-2\$. The greatest such positive integer \$H\$ is then \$\lfloor \sqrt{2 L^2-2}\rfloor\$, so the final result is \$ \lfloor \sqrt{2 L^2-2}\rfloor + 3 \$.
[Answer]
# [R](https://www.r-project.org/), ~~79~~ 77 bytes
```
L=scan();j=1:L;a=j*2^.5;b=Mod(j+j*1i+1i);3+2*sum(L>a)+max(a[L>a],b[L>b])%in%b
```
[Try it online!](https://tio.run/##K/qfpmCjq5BWmpdckpmfp5GpqVCtUJycmIciqpmpoBySWlyiUF6UWFCQWvTfxxakRkPTOsvW0MrHOtE2S8soTs/UOsnWNz9FI0s7S8swU9swU9PaWNtIq7g0V8PHLlFTOzexQiMxGsiM1UkCUkmxmqqZeapJ/2u5ioHG5lRqGFqZGuikaf4HAA "R – Try It Online")
*Note: as it turned out, this is completely destroyed by [xnor's formula](https://codegolf.stackexchange.com/a/231187/78274), which would be 23 bytes in R:*
```
(2*scan()^2-2)^.5%/%1+3
```
*However, I'm keeping the existing code as a reference to my original solution.*
### Original explanation
In general case, the most squares will be touched when the line is going close to the diagonal orientation. Specifically, the number of touched squares will be equal to \$2 \times x + 3\$, where \$x\$ is the number of unit square diagonals fully covered by the line. The lengths of square diagonals are stored in `a`.
However, in some cases a better coverage can be achieved by deviating from the diagonal orientation. To account for this, we also store a vector `b` containing the lengths of diagonals of "deviated" rectangles of size \$1 \times 2\$ up to \$L \times (L+1)\$ (instead of squares up to \$L \times L\$). It looks like we need to count one additional touch in those cases where the maximal value of `b` that is still smaller than \$L\$ exceeds the analogous value from `a`.
For example, the first few diagonals are of length:
```
 1x1       2x2       3x3       4x4
 1.414214  2.828427  4.242641  5.656854 ...
```
We can see that after going from \$L = 3\$ to \$L = 4\$ we have still covered only the 2nd term (\$2 \times 2\$ diagonal), it's not enough to reach the edge of the 3rd square, so we are not gaining any more touches. But if we switch to the "deviated" rectangles:
```
 1x2       2x3       3x4       4x5
 2.236068  3.605551  5.000000  6.403124 ...
 
```
Here \$L = 4\$ now encompasses a larger value of 3.60..., which corresponds to (\$2 \times 3\$) rectangle. In this orientation we gain an extra touched square compared to the diagonal.
[Answer]
# [J](http://jsoftware.com/), 28 bytes
*-1 thanks to Jonah!*
The optimal is always either the diagonal `L, L` with `3+2*L` tiles crossed as noted by @Kirill L. or `L, L+1`, in which case an extra tile is crossed.
```
((>]+&.*:>:)+3+2*])[<.@%%:@2
```
[Try it online!](https://tio.run/##Pc29SgNREEDh3qc4BGKyWXPZmbkz9wezBAQrK1tJJQkmjaDvz5rK4jRfc27LKm0uHDobnpjo9/aJl/e312W7nU/jY9r1uQ@jjbo7DR/P6bhe96Muw8P58@ubn/PvIXFZTcyda8Knf2ffMZxCpSGCKJIRRwpSkYYKqmhGHS1oRRsmmGIZcyywijWykJVsZCcHuZIbLrji94njgVe8EROhhBFOBFGJRpkoSrHlDw "J – Try It Online")
* `[<.@%%:@2` calculate `L` by dividing `n` by `sqrt(2)`, then flooring
* `3+2*]` `3+2*L`
* `]+&.*:>:` calculate length to `L, L+1`
* `> … +` if `n` is larger than this length, add 1
Because the lines have rational length `n`, and the diagonals irrational length `L`, we can always find an epsilon \$L - n > 0\$ to slide the line top-left to touch the 3 extra tiles, which justifies the `3+2*L`.
`L, L+2` will always be further away then the next diagonal `L+1, L+1`, as \$L^2 + (L+2)^2 = 2L^2 + 4L + 4 > 2L^2 + 4n + 2 = (L+1)^2 + (L+1)^2\$, so checking `L, L+1` is enough.
[Answer]
# [Vyxal](https://github.com/Lyxal/Vyxal), 7 bytes
```
*d⇩√3+⌊
```
[Try it Online!](https://lyxal.pythonanywhere.com?flags=&code=*d%E2%87%A9%E2%88%9A3%2B%E2%8C%8A&inputs=1&header=&footer=)
Shameless port of xnor's answer
```
*       # Square
 d      # Double
  ⇩     # Subtract 2
   √    # Square root
    3+  # Add 3
      ⌊ # Floor
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~18 17~~ 8 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
-9 using [xnor's mathematical simplification](https://codegolf.stackexchange.com/a/231187/53748) of the same method as the 17, below.
```
²Ḥ_2Æ½+3
```
A monadic Link that accepts a positive integer, \$L\$ and yields the maximal squares touched.
**[Try it online!](https://tio.run/##y0rNyan8///Qpoc7lsQbHW47tFfb@P///6YGAA "Jelly – Try It Online")** Or see the [test-suite](https://tio.run/##AR4A4f9qZWxsef//wrLhuKRfMsOGwr0rM//Dh@KCrP//NTA "Jelly – Try It Online").
### How?
```
²Ḥ_2Æ½+3 - Link: positive integer, L
²        - square -> L²
 Ḥ       - double -> 2L²
  _2     - subtract 2 -> 2L²-2
    Æ½   - integer square-root -> ⌊√(2L²-2)⌋
      +3 - add three
```
---
17:
```
²H½Ḟð‘Ḥ‘++²ḤƊ‘½<ʋ
```
**[Try it online!](https://tio.run/##y0rNyan8///QJo9Dex/umHd4w6OGGQ93LAGS2tqHNgFZx7qA7EN7bU51////39QAAA "Jelly – Try It Online")**
### How?
First finds the longest \$(a,a)\$ or \$(a,a+1)\$ diagonal that \$L\$ can cover with some to spare, then places the line along this diagonal, poking out at both ends, and shifts it in the \$x\$ direction to cover \$2a+3+X\$ squares where \$X=1\$ if the diagonal is \$(a,a+1)\$, otherwise \$X=0\$.
```
²H½Ḟð‘Ḥ‘++²ḤƊ‘½<ʋ - Link: positive integer, L
²                 - square -> L²
 H                - halve -> L²/2
  ½               - square-root -> side of square with diagonal L
   Ḟ              - floor -> a
    ð             - new dyadic chain, f(a,L)...
     ‘            - increment -> a+1
      Ḥ           - double -> 2a+2
       ‘          - increment -> 2a+3
                ʋ - last four links as a dyad, f(a, L):
            Ɗ     -   last three links as a monad, f(a):
          ²       -     square -> a²
         +        -     (a) add (that) -> a+a²
           Ḥ      -     double -> 2(a+a²)
             ‘    -   increment -> 2(a+a²)+1 = a²+(a+1)²
              ½   -   square-root -> diagonal length of (a,a+1)
               <  -   less than (L)? -> 1 or 0 -> X
        +         - (2a+3) add (X)
```
[Answer]
# [Perl 5](https://www.perl.org/), 73 bytes
```
for$d(0,1){$w=0;1while$w**2+($d+$w++)**2<$_**2;$m+=2*$w-1+$d}$_=int$m/2+1
```
[Try it online!](https://tio.run/##DcdBCoAgEAXQy/xFOUkqtCrP0sYiwVJKmEV09aY2D15ZzjSIrPlEaExn2xvszWh5i2kBK@WoQSAwUftnwvw7YifvFFhbQngw@3hU7L0jKzKYN5ca83GJLukD "Perl 5 – Try It Online")
Took @Kirill L.'s explanation and ran with it. Does two passes, without and with "deviation" in `$d`. The `int$m/2+1` outputs the average (rounded up if .5) of those two passes, which will be the max of those two results. Reads the wanted length from stdin and prints max number of squares for that length. The following bash command outputs max number of squares touched for all lengths 1 to 50:
```
for l in {1..50}; do
  echo $l | perl -pe \
  'for$d(0,1){$w=0;1while$w**2+($d+$w++)**2<$_**2;$m+=2*$w-1+$d}$_=int$m/2+1';
  echo -n " ";
done
3 5 7 8 9 11 12 14 15 17 18 19 21 22 24 25 27 28 29 31 32 34 35 36 38 39 41 42 43
45 46 48 49 51 52 53 55 56 58 59 60 62 63 65 66 68 69 70 72 73
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 10 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
Ò2nU²Ñ ¬ÄÄ
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=0jJuVbLRIKzExA&input=NTA)
```
Ò2nU²Ñ ¬ÄÄ     :Implicit input of integer U
Ò              :Negate the bitwise NOT of (i.e., floor and increment)
 2n            :  Subtract 2 from
   U²          :    U squared
     Ñ         :    Times 2
       ¬       :  Square root
        ÄÄ     :Add one twice
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 20 bytes
```
n=>(2*n*n-2)**.5+3|0
```
[Try it online!](https://tio.run/##VZjBcl5HEUbX0VMo3liyJ67p7umemYCSorJiBXtXFsLIQZSRUpYDC8hTsGDD0@VFzPkGlykWmqTSE/ne85//3jP@8@1fb5/evL//8cNXD49/vPv49ubjw803V/7i4cXDV3794sWrfBn/6B8/3D19@O726e7p5nVv0bLNttpuZs282WiWzWaz1Ww3t@befDTP5rP5ar5bWAtvMVpki2qxWuw2rA1vI9rINqqN1cZuaS29JX9ItqyWq@Vu1Vt5q2iVrarVarXb7G16m9Em11NtzjZ3W70tbyvayraqLS50t93b9raj7dF2tT3b5uJ754c76MEP99CLH@6i68aY6e6Mme7PmOkOdYvOzHXnzLhNc2bcqHGnFsy4Vwtm3K1xuxbMuGEbzLhl44ZtMOOubTDjvi2ZcefGrVuKJ7NklppxPcUMAlbMYGDFDAoGBpvMAGGT2dQHwfVMZtCwxQwetphBxEBiixlQbDMDi8HFNjPI2NZHyGcIG@98irDxzucIG@98kl2fLzM@doeN88E7bBw2rs8eNq5PHzauzx82LglQwGHjSOCwcdg4HjhsHBMcNg4bh43DxtHBYeMI4bBx2DhOeEorZrBx2DhiOGwcNRw2jhwOG4eN44fDxjHEYeNTPjKDjaOJw8YRxWHjsPElWWUrM9g4bBxhHDaOMr41Q2WcCdgEzgRsAmcCNtGlOTPYBM4EbAI2gTMBm3B9B5jhTMAmYBM4E/p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IE1omBZX5/cXF785r17eP7q/ub/qv7X98k68uX13@/@OL21Y8/Pf3pin979ruf3l/@5uHpb3fvn319@fbq/rrxH797fP/@7s2H/w0@/8XG6/vvteG3T5ef9nz76X@7vLn5v12X314@/@Xf/3x@@bX@@a/nF1/8fH3x88Wbx4enx3d3rz7c/uHd3dXt9cXn//Lu8YerZ7@/fXq6f/jh8vbdu/PbLt/o1337rF1@@fmXv3p6/Mvd1dVtu7@@@UZ/9Jc3t9f8oo//AQ "JavaScript (Node.js) – Try It Online")
Shamelessly copies the idea from [xnor's answer](https://codegolf.stackexchange.com/a/231187/100752)
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 27 bytes
```
:*2*2-0v
:})?v1+>::*{
;n+2<
```
[Try it online!](https://tio.run/##S8sszvj/30rLSMtI16CMy6pW077MUNvOykqrmss6T9vI5v///7plCoYGAA "><> – Try It Online")
Xnor's formula, but in a language with neither square root nor rounding operations. Instead, it does the equivalent thing of finding the least square number larger than \$2L^2 - 2\$
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 20 bytes (14 characters)
```
⌊√(2#^2-2)⌋+3&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n6Zg@/9RT9ejjlkaRspxRrpGmo96urWN1f4HFGXmlSg4KDgWFSVWRqfpKJgaxP4HAA "Wolfram Language (Mathematica) – Try It Online")
Shamelessly translating [xnor's Python answer](https://codegolf.stackexchange.com/a/231187/87058).
[Answer]
# [Java (JDK)](http://jdk.java.net/), 28 bytes
```
L->L+=Math.sqrt(2*L*L-2)+3-L
```
[Try it online!](https://tio.run/##JY5Ba8JAEIXv/opBCGyMWdqUXtQIHgsrPUhP0sM0yerEuNnuToRQ/O3piPDgPZjHN6/FG@ZtfZmqDmOEPZKDvxn44aejCiIji916quEqJ3XgQO50/AYMp5g@mtAKQQ9MnbaDq5h6pz8cfzkM46dvAnIfwEI5mXxrsnKPfNbxN7AqFmZh8iLN3nIzrQVkpajIMRCU8LoW25Tw/iIhy56v4DBGbq66H1h7GcJWzZOiXkFSJ26@BFqC1eh9N@6ibFCUpg/wfSa6T/8 "Java (JDK) – Try It Online")
Same as everyone, I guess, cheers to xnor!
#### Same length as:
```
L->(int)Math.sqrt(2*L*L-2)+3
```
[Try it online!](https://tio.run/##JY7BasJAEIbvPsUgBHaNWWqkl9oIHoVID9JT8TBNXDtp3Ky7EyGIz55OKAz8A/Pxzd/gHbOm/h2rFmOEA5KDxwx8/91SBZGRJe4d1XCVkzpyIHf5OgGGS9QTCY0YTM/UGtu7iqlzZu/402EYPvw5IHcBLBRjmW0VOdYH5B8Tb4FVvigXZZbrdD1uRGQFnAggKGC1kXgv4PVFljT9fwXHIfL5arqejZcibNU8yes3SOrEzZdAS7AGvW@HXZQOirSexM@ZzHP8Aw "Java (JDK) – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), ~~43~~ 29 bytes
```
.+
**2*
(^_|\1__)*__+
__$#1*
```
[Try it online!](https://tio.run/##K0otycxLNPyvp82lxeWjkhDPpaJnl8ClquGeABbTMtLi0oiLr4kxjI/X1IqP1@aKj1dRNtTi@v/f1AAA "Retina – Try It Online") Link is to test suite that generates the results from `1` to the input. Explanation: Now using @xnor's formula.
```
.+
**2*
```
Double the square of `L` and convert it to unary.
```
(^_|\1__)*__+
__$#1*
```
Subtract `2` and take the integer square root. This is based on @MartinEnder's comment to my Retina answer to [It's Hip to be Square](https://codegolf.stackexchange.com/questions/125237/) but without the leading `^` anchor as the subtraction of `2` guarantees a single match.
```
```
Add a final `1` and convert to decimal.
Previous 43-byte solution based on @KirillL.'s answer:
```
.+
**
((^(_)|\2__)*)\1(\2_(_))?.+
__$2$3$5
```
[Try it online!](https://tio.run/##K0otycxLNPyvp82lxeWjkhDPpaJnl8ClquGeABbT4tLQiNOI16yJMYqP19TSjDHUALKAApr2QOn4eBUjFWMVU67//00NAA "Retina – Try It Online") Link is to test suite that generates the results from `1` to the input. Explanation:
```
.+
**
```
Square `L` and convert it to unary.
```
((^(_)|\2__)*)\1(\2_(_))?.+
```
Find the largest `n` where `2n²<L²`, thereby dividing `L` by `√2`. Additionally, try to match a further `2n+1` (`\2` contains `2n-1`), because a rectangle of dimensions `n` by `n+1` has diagonal `√(2n²+2n+1)`. Match at least a further `1` so that the inequality is strict, but also because it guarantees a single match.
```
__$2$3$5
```
Calculate `2+2n`, plus add another `1` for the rectangular case. Note that `$2$3` is used because `\2` doesn't actually contain `2n-1` when `n=0`, but `$2$3` is always `2n`.
```
```
Add a final `1` and convert to decimal.
[Answer]
# [Stax](https://github.com/tomtheisen/stax), 7 [bytes](https://github.com/tomtheisen/stax/blob/master/docs/packed.md#packed-stax)
```
é╟φQRl:
```
[Run and debug it](https://staxlang.xyz/#p=82c7ed51526c3a&i=1%0A2%0A3%0A4%0A5%0A6&m=2)
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 7 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
n·Ít3+ï
```
Port of [*@xnor*'s Python answer](https://codegolf.stackexchange.com/a/231187/52210), so using the same formula:
$$f(L) = \lfloor\sqrt{L^2+2}+3\rfloor$$
[Try it online](https://tio.run/##yy9OTMpM/f8/79D2w70lxtqH1///b2oAAA) or [verify the first 50 test cases](https://tio.run/##yy9OTMpM/W9q4Opnr6Sga6egZO/3P@/Q9sO9Jcbah9f/1/kPAA).
**Explanation:**
```
n        # Square the (implicit) input-integer
 ·       # Double it
  Í      # Subtract it by 2
   t     # Take the square-root of that
    3+   # Increase it by 3
      ï  # Truncate/floor it to an integer
         # (after which the result is output implicitly)
```
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf/blob/master/math_golf.txt), 7 [bytes](https://github.com/maxbergmark/mathgolf/blob/master/code_page.py)
```
²∞⌡√3+i
```
Port of [*@xnor*'s Python answer](https://codegolf.stackexchange.com/a/231187/52210), so using the same formula:
$$f(L) = \lfloor\sqrt{L^2+2}+3\rfloor$$
[Try it online.](https://tio.run/##y00syUjPz0n7///Qpkcd8x71LHzUMctYO/P/f0MuIy5jLhMuUy4zLnMuCy5LLkMDLiNTLlMDAA)
**Explanation:**
```
²        # Square the (implicit) input-integer
 ∞       # Double it
  ⌡      # Subtract it by 2
   √     # Take the square-root of that
    3+   # Increment it by 3
      i  # Convert it to an integer
         # (after which the entire stack is output implicitly as result)
```
]