Pi Sequence II

View as PDF

Submit solution


Points: 10 (partial)
Time limit: 1.0s
Memory limit: 32M

Author:
Problem type
Allowed languages
Python

The number \pi is a mathematical constant that is the ratio of a circle's circumference to its diameter, approximately equal to 3.14159. The following series can be used to approximate this number (when k goes large):


\pi \approx \sum_{0}^{k}\frac{(-1)^i4}{2i+1}

In this programming problem, you are required to write a generator function named Pi_seq that generates the first k terms in the series. Additionally, you need to take advantage of the Fraction from the fraction module to avoid the precision error of built-in float in Python.

You can start with this template code and ensure your code can run correctly within this template:

from fractions import Fraction

def Pi_seq(n):
    # your code here

# Note that the following code is for local testing purposes only. You should leave this part of code unchanged and not submit it to the OJ system.
T = int(input())
for t in range(T):
    k = int(input())
    Pi = Fraction()
    for t in Pi_seq(k):
        Pi += t
    print(f'{Pi}')

Please note that you only have to submit the definition of the Pi_seq function to the OJ system along with the import statement if you used the module. The rest of the code is for your local testing purposes only.

Input Specification

The first line of the input gives the number of test cases, T, and then T test cases follow.

For each test case, it will consist of a single line that contains an integer k.

Constraints

1 \leq T \leq 100

1 \leq k \leq 1000

Output Specification

For each test case, output a single line containing the approximate value of \pi using the first k terms of the series.

Sample Input

2
1
10

Sample Output

4
44257352/14549535

Explanation

The 2 on the first line means three test cases follow.

The 1 on the second line means your generator should generate exactly the first term.

So the output of this test case should be 4.

The 10 on the third line means your generator should generate exactly the first ten terms.

So the output of this test case should be 4 - 4/3 + 4/5 - ... = 44257352/14549535.

Hint

generator Fraction


Comments

There are no comments at the moment.