The number 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 goes large):
In this programming problem, you are required to write a generator function named Pi_seq
that generates the first 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, , and then test cases follow.
For each test case, it will consist of a single line that contains an integer .
Constraints
Output Specification
For each test case, output a single line containing the approximate value of using the first 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 .
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 .
Hint
generator
Fraction
Comments