17
\$\begingroup\$

based off my previous challenge, this wikipedia article, and a Scratch project

Your task: given i, calculate \$\pi\$ till i terms of the Gregory-Leibniz series.

The series:

$$\pi=\frac{4}{1}-\frac{4}{3}+\frac{4}{5}-\frac{4}{7}+\frac{4}{9}-...$$

Here, 4/1 is the first term, -4/3 is the second, 4/5 is the second and so on.

Note that for the nth term,

  • $$\text S_n = \frac{4 \times (-1)^{n+1}}{2n-1}$$
  • $$\pi_n = \text S_1 + \text S_2 + ... + \text S_n,$$ where \$\pi_n\$ is \$\pi\$ approximated to \$n\$ terms.

Test cases:

In - Out
1 - 4
2 - 2.66666667
3 - 3.46666667
4 - 2.8952381

Floating point issues are OK.

You may not calculate infinite terms of pi using this as we are calculating a number rather than terms of a series here.

This is , so shortest answer wins!

EDIT: It's strange that this question got some new... activity.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ You may not calculate infinite terms of pi [...] -> Does that mean that we can't use standard sequence I/O? If so, I don't understand why. \$\endgroup\$
    – Arnauld
    Commented Sep 19, 2022 at 13:20
  • \$\begingroup\$ @Arnauld I think what he meant to say is that the challenge is to output the terms of the Gregory Leibniz sequence, and not the (potentially infinite) digits of pi itself. \$\endgroup\$ Commented Sep 19, 2022 at 13:22
  • \$\begingroup\$ Nope. Here pi is being calculated by adding up the terms of the series. So you add up the terms, and then print the approx. of pi when summing up \$\endgroup\$ Commented Sep 19, 2022 at 13:24
  • 1
    \$\begingroup\$ I don't understand why we cannot output \$\pi_n\$ with standard sequence I/O (this would mean also allowing output of approximations of \$\pi\$ up to \$n\$ terms or outputting more approximations without end). \$\endgroup\$
    – pajonk
    Commented Sep 20, 2022 at 5:26
  • \$\begingroup\$ Normally, I would have let you as you'd be printing terms of a series, but here we are printing approximations. Why should we print all approximations to a certain term? When calculating pi, we aim for the most accurate, not keeping a log of inaccurate periods. \$\endgroup\$ Commented Sep 20, 2022 at 7:01

37 Answers 37

1
2
0
\$\begingroup\$

Fig, \$11\log_{256}(96)\approx\$ 9.054 bytes

S\n2@N{hax4

Try it online!

I spent so long golfing this just to beat Husk. No hax used.

S\n2@N{hax4
        ax  # Range [1, n]
       h    # Double
      {     # Subtract 1
    @N      # Negate
  n2        # Every other element
 \        4 # 4 divided by the list
S           # Sum
\$\endgroup\$
1
0
\$\begingroup\$

PowerShell Core, 42 bytes

1.."$args"|%{$s+=(-4,4)[$_%2]/(2*$_-1)}
$s

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Alice, 44 bytes

/
 M /e!]4aaE*R!0~w~?R.![?2+.!]:+~t.$K;\ O @

Try it online!

Alice does not support decimal, so instead of doing decimal, I'll output an integer, starting from 4e10:

1 - 40000000000
2 - 26666666666
3 - 34666666666
4 - 28952380951

Please let me know if not OK and I'll withdraw the answer.

Explanation

/M/4aaE*R!]e![0~w~?R.!]?2+.![:+~t.$K;\O@   Flattened
/M/                                        Reads an argument and pushes it into the stack
   4aaE*R!]e![                             Write two entries `-40000000000` and `-1` in the tape
              0                            Pushes 0 on the stack, we will sum the other terms of the series onto it
               ~w~             ~t.$K       Loops until we have repeated the loop `argument` times
                  ?R.!]?2+.![              From the tape, negate the first argument and add `2` to the second and update the tape with their new values
                             :+            Divide them (forms the nth term of the series) and add it to the series' sum
                                    ;\O@   Outputs the result and finishes
\$\endgroup\$
0
\$\begingroup\$

jq, 32 bytes

[1+range(.*2)|drem(.;2)*4/.]|add

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

k, 17 bytes

+/(n#4 -4)%1+2*!n

1+2*!n generate the denominators

n#4 -4 generate the numerators

% vector division

+/ sum

\$\endgroup\$
0
\$\begingroup\$

PARI/GP 32 bytes

p(n)=sum(k=s=1,n,4/(1-2*k)*s=-s)
\$\endgroup\$
0
\$\begingroup\$

Thunno 2 S, 10 bytes

ıḌ⁻4\n⁺u@×

Try it online!

Explanation

ıḌ⁻4\n⁺u@×  # Implicit input
ı           # Map over [1..n]:
   4\       #  4 divided by...
 Ḍ⁻         #  (2 * n - 1)
         ×  #  Multiplied by
       u@   #  -1 to the power of...
     n⁺     #  (n + 1)
            # Implicit output of sum
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.