3 Replies - 1721 Views - Last Post: 18 July 2009 - 07:33 PM Rate Topic: -----

#1 lennin59  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-July 09

Fibonacci Closed Form

Post icon  Posted 18 July 2009 - 04:51 PM

I am having trouble getting the closed for of the Fibonacci series into code.
I know that is is a really simple thing, but some help would be appreciated. This is what I have thus far:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	double gr = ceil((1 + sqrt(5))/2);
	int n;
	cout << "Enter number: ";
	cin >> n;
	cout << ceil((pow(gr, n) - pow(1 - gr, n))/sqrt(5))5 << " is the " << n << "th Fibonnaci number.";
	system("pause");
	return 0;   
}




As far as I understand, this is how it is suppose to be, but it is not giving me the right answer.
The closed form of Fibonacci can be found here: http://en.wikipedia....form_expression

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Fibonacci Closed Form

#2 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Fibonacci Closed Form

Posted 18 July 2009 - 06:18 PM

View Postlennin59, on 18 Jul, 2009 - 05:51 PM, said:

	double gr = ceil((1 + sqrt(5))/2);

	cout << ceil((pow(gr, n) - pow(1 - gr, n))/sqrt(5))5 << " is the " << n << "th Fibonnaci number.";



If you remove the calls to ceil(), and get rid of that trailing 5 it seems to work.
Was This Post Helpful? 0
  • +
  • -

#3 lennin59  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-July 09

Re: Fibonacci Closed Form

Posted 18 July 2009 - 07:19 PM

View Postbrds, on 18 Jul, 2009 - 05:18 PM, said:

View Postlennin59, on 18 Jul, 2009 - 05:51 PM, said:

	double gr = ceil((1 + sqrt(5))/2);

	cout << ceil((pow(gr, n) - pow(1 - gr, n))/sqrt(5))5 << " is the " << n << "th Fibonnaci number.";



If you remove the calls to ceil(), and get rid of that trailing 5 it seems to work.


I tried fixing my code a bit and this is what I got now, but it still doesn't seem to be working. I need to get for 1-1, for 2-1, for 3-2, for 4-3, for 5-5, etc. And the answer needs to be integers corresponding to its number on the Fibonacci series.

Here is the code that has gotten the closest so far. It works well up to 4 and after that it gives values lowers than what it really is. I've tried everything I can think of, any help would be appreciated.

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
	double gr = (1 + sqrt(5))/2;
	int n;
	cout << "Enter number: ";
	cin >> n;
	cout << ceil(ceil(pow(gr, n) - pow(1 - gr, n))/ceil(sqrt(5))) << " is the " << n << "th Fibonnaci number.";
	system("pause");
	return 0;   
}


Was This Post Helpful? 0
  • +
  • -

#4 brds  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 76
  • View blog
  • Posts: 515
  • Joined: 22-October 08

Re: Fibonacci Closed Form

Posted 18 July 2009 - 07:33 PM

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double gr = (1 + sqrt(5))/2;
    int n;
    cout << "Enter number: ";
    cin >> n;
    cout << ((pow(gr, n) - pow(1 - gr, n))/sqrt(5)) << " is the " << n << "th Fibonnaci number.";
    system("pause");
    return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1