Skip to content

Instantly share code, notes, and snippets.

@barrucadu
Created October 26, 2011 12:05
Show Gist options
  • Save barrucadu/1316155 to your computer and use it in GitHub Desktop.
Save barrucadu/1316155 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#define PHI ((1 + sqrt (5)) / 2)
/**
* Get an integer above a given minimum value.
*
* @param The prompt to display
* @param The minimum value
*
* @return Integer in the range [min, INT_MAX]
*/
int
getint (char* prompt, int min)
{
int out;
do
{
printf (prompt);
scanf ("%i", &out);
}
while (out <= min);
return out;
}
int
main (void)
{
int result = 1, lastresult = 0, resultbeforelast = 0, increment, limit;
double phi, error;
limit = getint ("Enter upper bound greater than zero: ", 0);
do
{
printf ("%i \n", result);
resultbeforelast = lastresult;
lastresult = result;
result = result + increment;
increment = lastresult;
phi = (double)lastresult / (double)resultbeforelast;
error = fabs ((100 * (PHI - phi)) / PHI);
}
while (result <= limit);
printf ("Approximation of phi = %i / %i = %f \n", lastresult, resultbeforelast, phi);
printf ("True value of phi = %f\n", PHI);
printf ("Percentage error = %f%%\n", error);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment