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

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question
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
Transcribed Image Text: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
n=7
adding fraction: -1/15
3.01707181707
= 3.14159265359
.. adding fraction: 1/17
= 3.25236593472
= 3.14159265359
our pi
real pi
n=8
=
our pi
real pi
n=9 ... adding fraction: -1/19
our pi
real pi
3.04183961893
= 3.14159265359
=
n=10 ... adding fraction: 1/21
our pi
= 3.23231580941
real pi = 3.14159265359
Try this for M=10000. Remember, before computers, a person would have computed this by hand.
Isaac Newton (not using the method above) computed up to a precision of 16 decimal places in 1665.
The current record holder is Emma Haruka Iwao. She has estimated with over 31 trillion digits (and not by
hand!)
Transcribed Image Text:n=7 adding fraction: -1/15 3.01707181707 = 3.14159265359 .. adding fraction: 1/17 = 3.25236593472 = 3.14159265359 our pi real pi n=8 = our pi real pi n=9 ... adding fraction: -1/19 our pi real pi 3.04183961893 = 3.14159265359 = n=10 ... adding fraction: 1/21 our pi = 3.23231580941 real pi = 3.14159265359 Try this for M=10000. Remember, before computers, a person would have computed this by hand. Isaac Newton (not using the method above) computed up to a precision of 16 decimal places in 1665. The current record holder is Emma Haruka Iwao. She has estimated with over 31 trillion digits (and not by hand!)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Types of Loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning