## CS13002 Programming and Data Structures | ## Section 3/C, Spring 2003--2004 |

## Lab Test I : Solution

## For students with

oddPC numbers## The program

#include <stdio.h> #define LIMIT 1000000 /* Maximum number of terms */ int main() { double multiplier = 2 * 1.7320508076; /* 2 * sqrt(3) */ double threepower; /* Power of (-3) for the denominator */ double sum1, sum2; /* sum1 is the partial sum for approximation 1 */ /* sum2 is the partial sum for approximation 2 */ int n, m; /* n is the number of terms. m is for printing. */ /* Initialize */ n = 0; m = 10; sum1 = sum2 = 1; threepower = 1; /* Repeat for bigger and bigger values of n */ while (n <= LIMIT) { /* Consider the next term */ ++n; /* Add (-1)^n / (2n+1) to the first sum */ if (n%2) sum1 -= 1.0/(double)(2*n+1); else sum1 += 1.0/(double)(2*n+1); /* Add 1/[(-3)^n (2n+1)] to the second sum */ threepower *= (double)(-3); sum2 += 1.0/(threepower*(double)(2*n+1)); /* Check if printing is to be done */ if (n == m) { printf("n = %7d, approximation1 = %12.10lf, approximation2 = %12.10lf\n", n, 4*sum1, multiplier*sum2); m *= 10; /* Set value of n for next printing */ } } }

## Output

n = 10, approximation1 = 3.2323158094, approximation2 = 3.1415933046 n = 100, approximation1 = 3.1514934011, approximation2 = 3.1415926536 n = 1000, approximation1 = 3.1425916543, approximation2 = 3.1415926536 n = 10000, approximation1 = 3.1416926436, approximation2 = 3.1415926536 n = 100000, approximation1 = 3.1416026535, approximation2 = 3.1415926536 n = 1000000, approximation1 = 3.1415936536, approximation2 = 3.1415926536Look how fast the second series has converged, whereas the first series has not converged (with an accuracy of 10 decimal digits) even when one million terms are considered.

## For students with

evenPC numbers

## The program

#include <stdio.h> #define ERROR 0.00000000001 /* Error bound set at 10^(-11) */ int main () { double multiplier, /* The bracketed expression in the n-th term */ sum, oldsum; /* The current and the previous sums are maintained for checking convergence within the error limit. */ int n; /* Number of terms */ /* Initialize */ sum = multiplier = 0.5; oldsum = 0; n = 0; /* Repeat till the series converges with the desired accuracy */ while (1) { printf("n = %2d, s_n = %12.10lf, approximate value of pi = %12.10lf\n", n, sum, 6*sum); /* Consider the next term */ ++n; /* Obtain the value of the multiplier for this n from that for the previous n */ multiplier *= (double)(2*n-1) / (double)(8*n); /* Save previous sum */ oldsum = sum; /* Update sum by adding the new term */ sum += multiplier/((double)(2*n+1)); /* Check for convergence */ if (6*(sum-oldsum) < ERROR) exit(0); } }

## Output

n = 0, s_n = 0.5000000000, approximate value of pi = 3.0000000000 n = 1, s_n = 0.5208333333, approximate value of pi = 3.1250000000 n = 2, s_n = 0.5231770833, approximate value of pi = 3.1390625000 n = 3, s_n = 0.5235258557, approximate value of pi = 3.1411551339 n = 4, s_n = 0.5235851954, approximate value of pi = 3.1415111723 n = 5, s_n = 0.5235961193, approximate value of pi = 3.1415767158 n = 6, s_n = 0.5235982376, approximate value of pi = 3.1415894253 n = 7, s_n = 0.5235986637, approximate value of pi = 3.1415919824 n = 8, s_n = 0.5235987519, approximate value of pi = 3.1415925112 n = 9, s_n = 0.5235987705, approximate value of pi = 3.1415926229 n = 10, s_n = 0.5235987745, approximate value of pi = 3.1415926469 n = 11, s_n = 0.5235987754, approximate value of pi = 3.1415926521 n = 12, s_n = 0.5235987755, approximate value of pi = 3.1415926533 n = 13, s_n = 0.5235987756, approximate value of pi = 3.1415926535 n = 14, s_n = 0.5235987756, approximate value of pi = 3.1415926536 n = 15, s_n = 0.5235987756, approximate value of pi = 3.1415926536