# Calculating π using the Gregory Leibniz series until n terms

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.

• 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. Sep 19, 2022 at 13:20
• @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. Sep 19, 2022 at 13:22
• 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 Sep 19, 2022 at 13:24
• 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). Sep 20, 2022 at 5:26
• 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. Sep 20, 2022 at 7:01

# 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

• Aug 23, 2023 at 15:53

ABAP

    SELECTION-SCREEN BEGIN OF BLOCK b0.
PARAMETERS: p_in TYPE i OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b0.

START-OF-SELECTION.

DATA: v_result      TYPE float.
PERFORM calculate USING p_in CHANGING v_result.
WRITE / v_result.

FORM calculate USING v_in TYPE i
CHANGING v_result TYPE float.

DATA: v_exponent,
v_denominator,
v_next  TYPE i.

IF v_in = 1.
v_result = v_result + 4.
RETURN.
ELSE.
v_exponent = v_in + 1.
v_denominator = ( 2 * v_in ) - 1.
v_result = v_result + ( 4 * ( -1 ** v_exponent ) ) / v_denominator.
v_next = v_in - 1.
PERFORM calculate USING v_next CHANGING v_result.
ENDIF.


ENDFORM.

• Welcome to Code Golf, and nice first answer! Answers here need to be golfed (shortened), which you can do by removing whitespace and shortening variable names. Nov 16, 2022 at 19:05
• And besides you'll need to add your score in bytes. Nov 17, 2022 at 4:01

# PowerShell Core, 42 bytes

1.."$args"|%{$s+=(-4,4)[$_%2]/(2*$_-1)}
$s  Try it online! # 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


# jq, 32 bytes

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


Attempt This Online!

# k, 17 bytes

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


1+2*!n generate the denominators

n#4 -4 generate the numerators

% vector division

+/ sum

# PARI/GP 32 bytes

p(n)=sum(k=s=1,n,4/(1-2*k)*s=-s)


# Thunno 2S, 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