Question
Please write a C++ program 1. Pi (?) The mathematical constant Pi is an irrational number with valueapproximately 3.141592... The precise value of this constant
Please write a C++ program
1. Pi (?)
The mathematical constant Pi is an irrational number with valueapproximately 3.141592... The precise value of this constant can beobtained from the following infinite sum:
Pi2 = 8 + 8/32 + 8/52 + 8/72 + 8/92 + ...
(Pi is of course just the square root of this value.)
Although we cannot compute the entire infinite series, we get agood approximation of the value of Pi2 by computing the beginningof such a sum.
Please use both iterative way and recursive way in two differentfunctions to solve this problem. Write two functionsapproxPIsquared_iter() and approxPIsquared_recur() that takesnumbers included in the series as input and approximates constantPi2 .
Moreover, please use ctime to estimate how long does the processtake in both ways.
Notes:
1. The values returned by the functions are Pi2 , don?t forgetto find the square root.
2. Output the Pi to 12 decimal places.
Output :
How many numbers are in the series?:500 (iterative) By including 500 numbers in the series, the approximated Pi is 3.14095596951 It takes 6.1e-05 secs (recursive) By including 500 numbers in the series, the approximated Pi is 3.14095596951 It takes 8.4e-05 secs How many numbers are in the series?:50000 (iterative) By including 50000 numbers in the series, the approximated Pi is 3.14158628739 It takes 0.000279 secs (recursive) By including 50000 numbers in the series, the approximated Pi is 3.14158628739 It takes 0.001512 secs
Step by Step Solution
3.40 Rating (150 Votes )
There are 3 Steps involved in it
Step: 1
C Code Importing libraries include include include include using namespace ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started