Discussion: Calculating Nested Values
Execute the code to understand the output and gain insights into recursive computation.
We'll cover the following...
Run the code
Now, it's time to execute the code and observe the output.
#include <stdio.h>float p(float a, float b){return((b<100.0)?a+(b*b/p(a+2.0, b+1.0)):b*b);}int main(){printf("%.7f\n", 4.0/p(1.0, 1.0));return(0);}
Understanding the output
3.1415925
How the code generates this output, especially without using any trigonometric functions, is the story to tell.
The story
Calculating π (pi), the ratio of a circle’s diameter to its circumference, is an age-old computer programming task. It was also the subject of a Star Trek episode where Mr. Spock directed the computer to “calculate to the last digit the value of pi.” Of course, the computer got busy with the project to the point of pushing out an evil entity. Computers can do amazing things!
The question isn’t whether a computer can calculate the value of π but how? To assist the programmer, various mathematical theorems are available. Each of ...