[Question]
  [
Your task: given a nonzero positive number `i`, calculate pi using the Nilakantha series unto `i` terms.
The Nilakantha series is as follows:
$$\text 3 + \frac{4}{2 \times 3 \times 4} - \frac{4}{4 \times 5\times 6}+\frac{4}{6 \times 7 \times 8} - ...$$
3 is the first term, `4/2*3*4` is the second, `-4/4*5*6` is the third, and so on.
Notice that for the nth term:
* $$\text S\_1 = 3$$
* $$\text S\_n = \frac{4 \times (-1)^n}{2n \times (2n-1) \times (2n-2)}$$
* The approximation of pi by summing up these terms is $$\text S\_1 +\text S\_2\text + … +\text S\_n$$
Test cases:
```
In = Out
1 = 3
2 = 3.16666666667
3 = 3.13333333333
4 = 3.1452381
```
Notice the pattern of the numbers approximating towards pi.
Floating point issues are OK.
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so shortest answer wins!
**EDIT**: by default this is 1-indexed, but if you want 0-indexed no problem, just mention it. And even infinitely printing the approximations with no input is Okay.
**EDIT 2**: Why some activity here?
  
[Answer]
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 38 bytes (@xnor)
```
f=lambda n,i=.5:i//n/n or 2/i-f(n,i+1)
```
[Try it online!](https://tio.run/##DchBCoAgEADAe6/Yo0vWohGE4GOMshZqFfHS6805Tv7qnWTZcmkt@ie8@xFANPt5dUwkJJAKWOIpqt6jwRZ7MLBACXKdymhr0A2QC0tVUTFi@wE "Python 3.8 (pre-release) – Try It Online")
### [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 40 bytes (@xnor)
```
f=lambda n,i=1:i//2//n/n or 4/i-f(n,i+2)
```
[Try it online!](https://tio.run/##DcnBCoAgDADQe1@x4yRjaB1C6GOMWg1qinjp683re/mrd9J5zaU13p747kcEtbK5IESeSEkhFVhIJsYeozeNOwiIQol6neisdyYMkItoRUYxpv0 "Python 3.8 (pre-release) – Try It Online")
### [Python](https://www.python.org), 45 bytes
```
f=lambda n,s=1:4/(2*s-1)-(s//n/s or f(n,s+1))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwYKlpSVpuhY3ddNscxJzk1ISFfJ0im0NrUz0NYy0inUNNXU1ivX18_SLFfKLFNI0gJLahpqaUE1KBUWZeSUaaRqGmjppGkYgwhhEGBpASAOYSpg1AA)
1-based. n has to be positive.
### [Python](https://www.python.org), 50 bytes
```
f=lambda n,s=0:n and f(n-1,4/(n+n-1)-s-0**s/n)or s
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwYKlpSVpuhY3jdJscxJzk1ISFfJ0im0NrPIUEvNSFNI08nQNdUz0NfK0gQxN3WJdAy2tYv08zfwihWKoTqWCosy8Eo00DQNNnTQNQxBhBGZB-AYGmpoQlTC7AA)
This uses \$\frac 4 {2x(2x+1)(2x+2)} =\frac 1 x + \frac 1 {x+1} - \frac 4 {2x+1} \$
and that inside the full sum the first two terms cancel.
1-based. Can handle 0.
# [Python 3.8 (pre-release)](https://docs.python.org/3.8/), 45 bytes (@xnor)
```
f=lambda n:0**n*3or(-1)**n/n/(n-~n)/~n+f(n-1)
```
[Try it online!](https://tio.run/##DcYxCoAwDAXQ3VNkTCpSaxcRPExFqwX9LcHFxatX3/TKcx8Zfixaa5zPcC1rIEy9MTA@K3dO/lpYRvdC7Is2/nVSY1ZKlEAasG889DI1VDTh5shJpH4 "Python 3.8 (pre-release) – Try It Online")
### [Python](https://www.python.org), 50 bytes
```
f=lambda n:0**n*3or 1/(n|1)/(~n-n)/(n%-2^n)+f(n-1)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72soLIkIz9vwYKlpSVpuhY3jdJscxJzk1ISFfKsDLS08rSM84sUDPU18moMNfU16vJ084BUnqquUVyepnaaRp6uoSZUp1JBUWZeiUaahoGmTpqGIYgwArMgfAMDTahKmF0A)
0-based.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 13 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
RḤrƝP€4÷Ṛḅ-+3
```
A monadic Link that accepts a positive integer and yields the approximation (up to the floating point accuracy).
**[Try it online!](https://tio.run/##ASIA3f9qZWxsef//UuG4pHLGnVDigqw0w7fhuZrhuIUtKzP///80 "Jelly – Try It Online")**
### How?
```
RḤrƝP€4÷Ṛḅ-+3 - Link: positive integer, n  e.g. 4
R             - range                           [1,2,3,4]
 Ḥ            - double                          [2,4,6,8]
   Ɲ          - for neighbours:
  r           -   inclusive range               [[2,3,4],[4,5,6],[6,7,8]]
    P€        - product of each                 [24,120,336]
      4÷      - four divided by those           [1/6,1/30,1/84]
        Ṛ     - reverse                         [1/84,1/30,1/6]
         ḅ-   - convert from base -1            sum([1/84,-1/30,1/6])=0.14523809523809522
           +3 - add three                       3.14523809523809522
```
[Answer]
# JavaScript (ES6), 32 bytes
This version is based on [@loopy-walt's answer](https://codegolf.stackexchange.com/a/251927/58563), golfed by [@xnor](https://codegolf.stackexchange.com/users/20260/xnor).
```
f=(n,i=.5)=>i<n?2/i-f(n,i+1):1/n
```
[Try it online!](https://tio.run/##FYxBCoMwEADvvmIPHrJETVPwoqZ@xZCaEpFd0dJLyNvTeBoYhtnsz17uDMe3JX6vOXsjqAmm69G8wkTzU4XW30pqHLSi7PkUBAb0CAQT6EehlAixAnBMF@9rt/NHLFbUkRKWtI7lgGnBsUr5Dw "JavaScript (Node.js) – Try It Online")
---
# JavaScript (ES6), 39 bytes
```
f=n=>--n?f(n)+(4-n%2*8)/(n+=n)/++n/~n:3
```
[Try it online!](https://tio.run/##FcxBCsIwEEbhfU8xiwozDjFWXYg1epWG2ohS/kgrbkK8eqyrt/l4T//xcz89Xm@DeBtKCQ7uYgyugSHKB4PVbn0Uy1AHsaqwX5z2JcSJQY6alkBnarZLVYVSRdRHzHEcNmO8c@e5Tsiy0Dr9n7mTtsrlBw "JavaScript (Node.js) – Try It Online")
### Commented
```
f = n =>          // f is a recursive function taking the input n
--n ?             // decrement n; if it's not 0:
  f(n) +          //   do a recursive call and add:
  (4 - n % 2 * 8) //     -4 if n is odd, +4 otherwise
  / (n += n)      //     divided by 2n
  / ++n           //     divided by 2n + 1
  / ~n            //     divided by -(2n + 2)
:                 // else:
  3               //   end of recursion: return the integer part
```
[Answer]
# [Factor](https://factorcode.org) + `koszul math.unicode`, ~~68~~  64 bytes
```
[ ""3 rot [0,b) [ -1^ 4 reach Π / * + [ 2 v+n ] dip ] each ]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=JYw5CsJAGIVBrXKKRzr3JRaiIHZiYyM2hgjj-McEx5k4mQh6FZs0egHvYZ_bmDjN2z54z3fIuFE6L3bbzWq9nOJMWpJASteMJKcUZ5U-MoELM1EvkzFXR7JFM3kqeZXtcKPqKkWiyZh7omNpMHMc75WZsDspFj7cWr3hetDKwB90Dk346A73GEMT4xG-OfpooV3OI9zaEgGOcVLqnwb258OZEJiHQjGDHrggpi3Jc-s_)
0-indexed. Note the string literal has the control characters 2, 3, and 4 embedded, making it equivalent to the sequence `{ 2 3 4 }`. You can see these characters on ATO.
```
              ! 3
""            ! 3 { 2 3 4 }
3             ! 3 { 2 3 4 } 3
rot           ! { 2 3 4 } 3 3
[0,b)         ! { 2 3 4 } 3 { 0 1 2 }
[ ... ] each     <<for each element in { 0 1 2 }...>>
                 <<first iteration>>
              ! { 2 3 4 } 3 0
-1^           ! { 2 3 4 } 3 1
4             ! { 2 3 4 } 3 1 4
reach         ! { 2 3 4 } 3 1 4 { 2 3 4 }
Π             ! { 2 3 4 } 3 1 4 24
/             ! { 2 3 4 } 3 1 1/6
*             ! { 2 3 4 } 3 1/6
+             ! { 2 3 4 } 3+1/6
[ 2 v+n ] dip ! { 4 5 6 } 3+1/6
                 <<second iteration>>
              ! { 4 5 6 } 3+1/6 1
                 <<and so on>>
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `ḋ`, 19 bytes
```
ƛune4*nd:‹:‹**/;∑3+
```
`ḋ` flag to print rationals in their decimal form
**Explanation:**
```
ƛune4*nd:‹:‹**/;∑3+
ƛ              ;      Map lambda through inclusive range 1 to input
 une                  Push -1 to the power n
    4*                Multiply by 4 and push that
      nd:             Multiply n by 2 and duplicate
         ‹:           Decrement and duplicate
           ‹**        Decrement and push product of denominator
              /       Divide 4*(-1)**n by 2n*(2n-1)*(2n-2)
                ∑3+   Sum list and add 3
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyLhuIsiLCIiLCLGm3VuZTQqbmQ64oC5OuKAuSoqLzviiJEzKyIsIiIsIjQiXQ==)
[Answer]
# [R](https://www.r-project.org), 51 bytes
```
\(k,n=2:k*2)`if`(k>1,3+sum(4*1i^n/n/(n-1)/(n-2)),3)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72waMGiNNulpSVpuhY3jWM0snXybI2ssrWMNBMy0xI0su0MdYy1i0tzNUy0DDPj8vTz9DXydA01QaSRpqaOsSZE6-Y0DUNNrjQNIxBhDCJMoDILFkBoAA)
Uses the fact that \$(-1)^n=i^{2n}\$.
---
### [R](https://www.r-project.org), 51 bytes
```
\(k,n=2:k)`if`(k>1,3+sum((-1)^n/n/(2*n-1)/(n-1)),3)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72waMGiNNulpSVpuhY3jWM0snXybI2ssjUTMtMSNLLtDHWMtYtLczU0dA014_L08_Q1jLTygGx9DRCpqWOsCdG6OU3DUJMrTcMIRBiDCBOozIIFEBoA)
Uses the formula but with simplifying the fraction to \$\frac{(-1)^n}{n(2n-1)(n-1)}\$.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~20~~ 16 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
3λè®Nm4*N·2Ý-P/+
```
Outputs the 1-based \$n^{th}\$ value (by starting with \$a(0)=3\$ and where \$a(1)\$ is calculated as \$3\$ as well).
[Try it online](https://tio.run/##yy9OTMpM/f/f@NzuwysOrfPLNdHyO7Td6PBc3QB9baAwAA) or [verify the infinite sequence](https://tio.run/##yy9OTMpM/f/f@NzuQ@v8ck20/A5tNzo8VzdAX7v20LL//wE).
**Explanation:**
```
 λ         # Start a recursive environment,
  è        # to calculate a(input)
           # (which is output implicitly afterwards)
3          # Start with a(0)=3
           # Where every following a(n) is calculated by:
           #  (implicitly push the previous term a(n-1))
   ®Nm     #  Push (-1) to the power n
      4*   #  Multiply it by 4
   N·      #  Push 2n
     2Ý    #  Push list [0,1,2]
       -   #  Subtract each from the 2n: [2n,2n-1,2n-2]
        P  #  Take the product of this triplet: 2n*(2n-1)*(2n-2)
   /       #  Divide the earlier 4*(-1)**n by this (2n*(2n-1)*(2n-2))
    +      #  Add it to the previous term a(n-1)
```
[Answer]
# [><>](https://esolangs.org/wiki/Fish), 36 bytes
Prints the infinite sequence
```
23:nao$:1+$:2%8*4$-$2*:1-:1-**,{+10.
```
[Try it online!](https://tio.run/##S8sszvj/38jYKi8xX8XKUFvFykjVQstERVfFSMvKUBeItLR0qrUNDfT@/wcA "><> – Try It Online")
# [><>](https://esolangs.org/wiki/Fish), 41 bytes
Generates the **nth** term
```
3$v ;n~<
1->:2(?v::2%8*4$-$2*:1-:1-**,{+}
```
[Try it online!](https://tio.run/##S8sszvj/31ilTME6r86Gy1DXzspIw77MyspI1ULLREVXxUjLylAXiLS0dKq1a////69bZgIA "><> – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~80~~ \$\cdots\$ ~~71~~ 70 bytes
```
i;float s,m;float f(n){for(m=4,s=i=3;i++<2*n;s-=m/i/~-i/(i++-2))m=-m;}
```
[Try it online!](https://tio.run/##XZFtT8MgEMff@ykuTZbAWqxtNx/C6hvjp3B7gQwmcaVNIbFxqR/dSimr00sg3P/ufrk7ODlwPgyKymPNLJikCi@JND7JukVVuUpMqcqCqjje5EtNDSmrVKVfRKXIaSTHuCpJRftBaQsVUxphOF2Bs4klukZwK/YvOyjhVCRQXGe3Z7vzbjGbd1frvLjPeuohvNbGAn9j7dLdgr@LdiJF2@4533YPT@6sowQu/SIK1aGFxqhjrV1VJsjqMsLKPLh1C2icQOm96FzmDQ3PDRj1KWqJzoPgNAjLWaEQxz4be9g0/mgj0TrahIoho3Mo7NwFJbL4vy6cPm/OV@/oH2rDjK9lrwYJIGCw6zQM@pvZtC5XomixB/IICwkLs9VuWTZxvz3vc2TtQgv9VT98c3lkBzOQjx8 "C (gcc) – Try It Online")
*Saved ~~5~~ 6 bytes thanks to [Kevin Cruijssen](https://codegolf.stackexchange.com/users/52210/kevin-cruijssen)!!!*  
*Saved 4 bytes thanks to [Arnauld](https://codegolf.stackexchange.com/users/58563/arnauld)!!!*
[Answer]
# [Desmos](https://www.desmos.com/), 36 bytes
```
f(x)=3+∑_{n=2}^x(-1)^n/n(2nn-3n+1)
```
[Try it on Desmos!](https://www.desmos.com/calculator/lv5parlrnm)
Near direct copy of the definition, simplified and rearranged only a little bit. 1 indexed.
Breakdown:
```
f(x)=3+∑_{n=2}^x(-1)^n/n(2nn-3n+1)  full function
f(x)=                               function definition (not sure if required)
     3+                             3 plus
       ∑                            The sum from
        _{n=2}                        n=2
              ^x                      to n=x
                                      (note that this defaults to 0 if x is less than 2)
                                    of
                (-1)^n                -1 when n is odd, 1 if n is even
                      /               divided by
                       n(2nn-3n+1)    2n^3-3n^2+n
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal) `ḋr`, 14 bytes
```
Ḣƛd2ʀεΠ4/;uβ3+
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyLhuItyIiwiIiwi4biixptkMsqAzrXOoDQvO3XOsjMrIiwiIiwiNCJd)
```
Ḣ              # range(2, n)
 ƛ       ;     # map...
     ε         # Difference of...
  d            # 2x
   2ʀ          # [0, 1, 2] 
      Π        # Take the product
       4/      # 4 / that
          uβ   # Convert from base -1 (alternate signs)
            3+ # Add 3
```
[Answer]
# [Python 3](https://docs.python.org/3/), 49 bytes
```
f=lambda n:n-1and(-1)**n/(2*n*n-n)/~-n+f(n-1)or 3
```
[Try it online!](https://tio.run/##DcoxDoAgEAXR3lNsuYsSXe1IPAwGURP9EmJj49WRZpo36X32G1MpcT79tQRPcLDqEdiqGIOeRwMDC@k/izZyVbkzTSXWgg5Q9thW1k4HcQ1RygcerqNI@QE "Python 3 – Try It Online")
-5 bytes thanks to Mukundan314
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), ~~36~~ 35 bytes
```
3.-Sum[(-1)^n/(2n^3+3n^2+n),{n,#}]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b731hPN7g0N1pD11AzLk9fwygvzljbOC/OSDtPU6c6T0e5Nlbtv0t@dEBRZl5JdKauXVp0ZmysjkJ1po6Bjmlt7H8A "Wolfram Language (Mathematica) – Try It Online")
Alternatively, and more interestingly, we can express the partial sums in terms of a [Lerch transcendent](https://functions.wolfram.com/ZetaFunctionsandPolylogarithms/LerchPhi/): 36 bytes
```
Pi+(-1)^#(1/#-2LerchPhi[-1,1,#+.5])&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7PyBTW0PXUDNOWcNQX1nXyCe1KDkjICMzWtdQx1BHWVvPNFZT7b9LfnRAUWZeSXSmrl1adGZsrI5CdaaOaW3sfwA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# x86 32-bit machine code, ~~37~~ 33 bytes
```
D9 E8 D8 C0 D8 C0 D9 E8 F9 D9 E8 D8 F1 D9 E8 DE C2 D8 F1 D8 C0 F5 72 F5 DE E2 E2 ED DD D8 D9 E1 C3
```
[Try it online!](https://tio.run/##bVJdbyIhFH2WX3E7GxPQ0ahNmkY7/QXbl33apG0Mw8fIBsEAY8c0/es7vTPWrWvLA4RzD@ceDohJJUTb/jBO2FoquItJGj/d3JP/IGvKSywYV3UYWa95wl1ZJ7VeU6p5TIJbyxhY7yqQvi6tAk2NS@DYCvbeSFBOUrYiPG6Bwq@MkmllfcktaKKXA23lnAw0lxJiojOWH5fvoZ6LPUn4d1Ca/RlrzogJ56K7Dl2cSXzhf9dIbAUZ/BFgApZjXYZLGev9Dvpi2n0aLiMZBJUI3nhJWAZ4a9JFseXG9ZnwUIkcxIYHGI1ws2fklQCOrthAAbMcDt2y6tGXjcE46XjcwF0B8xvWo93Y4ZMkTbPhwsDkHobT@e1P/eSyHJoc828YOyqceE/ugYuNcQqEl2qZfZSbz17Sw@uJDcPZ4jdqHQpKaxdN5ZTsXY@YZo/NePzMVm8nd18Y6PaqgEsYM7nwBHRooDwkFdnR@UcdE6yD63y9kbb9K7TlVWwn2@sFTviLCjyu7Ds "C (gcc) – Try It Online")
Following the `fastcall` calling convention, this takes a number `i` in ECX and returns the sum of the first `i` terms on the FPU register stack.
In assembly:
```
f:  fld1
    fadd st(0), st(0)
    fadd st(0), st(0)   # Example execution for 2nd iteration
    fld1                # FPU register stack (left is bottom):
    stc                 # -3   2   (before)
r:  fld1                # -3   2   1
    fdiv st(0), st(1)   # -3   2   1/2
ir: fld1                # -3   2   1/2   1
    faddp st(2), st(0)  # -3   3   1/2
    fdiv st(0), st(1)   # -3   3   1/(2*3)
    fadd st(0), st(0)   # -3   3   2/(2*3)
    cmc                 #[Repeat the 4 instructions above, using an inner loop:
    jc ir               # -3   3   2/(2*3)   1
                        # -3   4   2/(2*3)
                        # -3   4   2/(2*3*4)
                        # -3   4   4/(2*3*4)
    fsubrp st(2), st(0) # 3+4/(2*3*4)   4
    loop r
    fstp st(0)
    fabs
    ret
```
Each iteration of the outer loop computes one term \$\frac4{2n(2n+1)(2n+2)}\$ and combines it in using the reverse-subtract instruction; as in [my answer to a related problem](https://codegolf.stackexchange.com/a/252080/104752), this handles the alternating signs, but leaves the result with the wrong sign for even `i`, which is corrected for by taking the absolute value at the end (because all the correct results are positive).
The first iteration, if left the same as later iterations, would contain a division by 0. This is prevented by setting CF to 1 at the beginning, so that the inner loop executes once instead of twice during the first iteration, and it computes \$\frac2{1\cdot2}\$ instead.
[Answer]
# [Raku](https://raku.org/), 36 bytes
```
[\+] 3,{-4*($*=-1)/[*] ^3+2*++$}...*
```
[Try it online!](https://tio.run/##K0gtyjH7n1up4FCsYPs/OkY7VsFYp1rXREtDRctW11BTP1orViHOWNtIS1tbpVZPT0/rv7VCcWKlgpJKvIKtHVBbtEp8rJJCWn6RQpyhwX8A "Perl 6 – Try It Online")
This is an expression for the infinite sequence of partial sums. 3 is the first term of the sequence, and the curly braces enclose a generating expression for the subsequent terms.
* The dividend is `-4 * ($ *= -1)`. The `$` here is an anonymous state variable. The `*= -1` causes it to alternate between -1 and 1. (The first time the expression is evaluated, it's undefined, but since it's being multiplied, it defaults to the multiplicative identity element 1.) Multiplying that by -4 produces the sequence of dividends 4, -4, 4, -4, ....
* The divisor is `[*] ^3 + 2 * ++$`. `$` here is another anonymous state variable which the preincrement operator `++` causes to take on the values 1, 2, 3, ..., as it's evaluated for each term of the sequence. Multiplying that by 2 produces 2, 4, 6, .... Those even numbers are added to the range `^3`, which means the integers from 0 to 2, producing a sequence of ranges `2, 3, 4`, `4, 5, 6`, `6, 7, 8`, .... Then `[*]` multiplies those numbers together.
`[\+]` produces the sequence of partial sums of the terms.
[Answer]
# [Fig](https://github.com/Seggan/Fig), \$18\log\_{256}(96)\approx\$ 14.816 bytes
```
+3S\n2@Nere+r3hax4
```
[Try it online!](https://fig.fly.dev/#WyIrM1NcXG4yQE5lcmUrcjNoYXg0IiwiMyJd)
Port of Vyxal. Beats both that and osabie, 1.8 bytes longer than Jelly. 0-indexed.
```
+3S\n2@Nere+r3hax4
               ax  # Range [1, n]
              h    # Double
            r3     # [0, 1, 2]
          e+       # Add ^ to every element of ^^
        er         # Product of each element
      @N           # Negate
    n2             # Every other element
   \             4 # Four divided by ^
  S                # Sum
+3                 # Add 3
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~22~~ 21 bytes
[Try it online!](https://tio.run/##TYzNCsIwEIRfpccN1Iv25tFceqgI9QXSZMGF/JRtUvDptwkiepmB@WbGvgzbZLzIgylmuJktw1wCTGaFMa4l30tYkEH13RM5Gn4D9Z2mnRyCRssYMGZ0oFNZfPUpueJTK52VaisKuMFHazjG34Qa/w@@HxU0dGmmriKDnHZ/AA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
   Ｎ                     Input `n` as a number
  Ｅ                      Map over implicit range
     ι                   Current value
    ⎇                    If nonzero then
          ι              Current value
         ﹪               Modulo
           ²             Literal integer `2`
        ⊗                Doubled
       ⊖                 Decremented
      ∕                  Divided by
              ι         Current value
             ×          Multiplied by
                ι       Current value
               ⊕        Incremented
            ×           Multiplied by
                   ι    Current value
                  ⊗     Doubled
                 ⊕      Incremented
                     ³   Otherwise literal integer `3`
 Σ                       Take the sum
Ｉ                        Cast to string
                       Implicitly print
```
Edit: Saved 1 byte by adpating @pajonk's simplification.
]