Task 4 [Computing Pl via Leibniz series] This task is to help you use for-loops for mathematical expressions in code. It is an irrational number that means it cannot be expressed as a fraction. Ever wonder how we compute ? One of the pioneers of the mechanical calculator, Gottfried Wilhelm von Leibniz, proved that could be expressed with the following infinite series: 1 1 1 1 1 1 1 + + 35-7 1 1 1 + 9 11 13 15 17 19 TT 4 Notice the alternating plus/minus sign in the series. We call each of the fractions in the series a "term." To compute π requires the summing of an infinite number of terms; however, in practice, this is not possible. Instead, we can only approximate the true value using a finite number of terms. Using the Leibniz formula, the more terms we compute in the series, the more accurate our calculation of π. Your task is to compute π up to a user-specified number of terms (i.e., up to M terms). Don't get scared of math, it is easy once you understand it. The Σ (sigma) symbol is a mathematician's way of We can rewrite Leibniz's series in a more compact form as
Types of Loop
Loops are the elements of programming in which a part of code is repeated a particular number of times. Loop executes the series of statements many times till the conditional statement becomes false.
Loops
Any task which is repeated more than one time is called a loop. Basically, loops can be divided into three types as while, do-while and for loop. There are so many programming languages like C, C++, JAVA, PYTHON, and many more where looping statements can be used for repetitive execution.
While Loop
Loop is a feature in the programming language. It helps us to execute a set of instructions regularly. The block of code executes until some conditions provided within that Loop are true.
![Task 4 [Computing Pl via Leibniz series]
This task is to help you use for-loops for mathematical expressions in code.
It is an irrational number - that means it cannot be expressed as a fraction. Ever wonder how we compute ?
One of the pioneers of the mechanical calculator, Gottfried Wilhelm von Leibniz, proved that could be
expressed with the following infinite series:
1-
1 1 1
5 7 9 11 13 15 17 19
1 1 1 1 1 1
+
+
3
We can rewrite Leibniz's series in a more compact form as
follows:
π = 4 x
Notice the alternating plus/minus sign in the series. We call each of the fractions in the series a "term." To
compute requires the summing of an infinite number of terms; however, in practice, this is not possible.
Instead, we can only approximate the true value using a finite number of terms. Using the Leibniz formula, the
more terms we compute in the series, the more accurate our calculation of . Your task is to compute π up
to a user-specified number of terms (i.e., up to M terms).
Don't get scared of math, it is easy once you understand it.
The Σ (sigma) symbol is a mathematician's way of
expressing a "for loop" for summing up terms. This
symbol means the following:
sum=0
for n
M
Σ
n=0
(-1)"
2n + 1
Your task should work as follows:
(1) Ask the user for the number of terms, M.
(2) Compute pi based on Leibniz formulation.
(3) At each iteration of your for-loop, print out the result
(i.e., your current sum x 4)
Print your results with precision 12.
Compare your result with 3.14159265359.
See example output below:
our pi= 3.33968253968
real pi= 3.14159265359
n=5
---- Task 4: Compute PI
Input number of terms, M: 10
n=0
adding fraction: 1/1
our pi4.00000000000
real pi= 3.14159265359
n=1
adding fraction: -1/3
our pi 2.66666666667
real pi= 3.14159265359
n=2
adding fraction: 1/5
our pi= 3.46666666667
real pi= 3.14159265359
n=3
our pi 2.89523809524
real pi= 3.14159265359
n=4
adding fraction: -1/7
adding fraction: 1/9
adding fraction: -1/11
our pi 2.97604617605
real pi= 3.14159265359
n=6
adding fraction: 1/13
our pi= 3.28373848374
real pi= 3.14159265359
+ ...=
sum
range (0,M+1):
sum + [term]
Here, [term] is the result of the expression in
blue, evaluated using the current value of n in the
for loop. For example, if M=2, we have:
TT
4
n=0, we have
n=1, we have
(-1)⁰
2*0+1
(-1)¹
2+1
1
2*1+1
(-1)²
n=2, we have
2*2+1
4+1
(Look familiar? This is Leibniz's series.)
3
- +
1
5](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fdac761a6-ba1c-4b50-93cd-f82ea0746880%2F749beba1-df8b-44bd-a36d-1f11479e0ddd%2Fbmqpxd_processed.png&w=3840&q=75)


Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images









