Search⌘ K
AI Features

Discussion: More Math, but Fun This Time

Explore the concept of the golden ratio and learn how to implement its calculation using recursive functions in C. This lesson helps you grasp recursion and mathematical expressions as applied in programming puzzles.

Run the code

Now, it's time to execute the code and observe the output.

C
#include <stdio.h>
double phi(double p, int precision)
{
while(precision)
return( p + 1/phi(p, precision-1) );
return(p);
}
int main()
{
double gr;
gr = phi(1.0, 15);
printf("Phi is %f\n", gr);
return(0);
}

Understanding the output

The code outputs the following line:

Phi is 1.618034
Code output

This is the golden ratio, represented by Greek letter phi, ϕϕ ...