# Fibonacci Closed Form

Page 1 of 1

## 3 Replies - 5087 Views - Last Post: 18 July 2009 - 07:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=115550&amp;s=13cadb223809077890bef8a31413d4e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lennin59

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

# Fibonacci Closed Form

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

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

## Re: Fibonacci Closed Form

Posted 18 July 2009 - 06:18 PM

lennin59, 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.

### #3 lennin59

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

## Re: Fibonacci Closed Form

Posted 18 July 2009 - 07:19 PM

brds, on 18 Jul, 2009 - 05:18 PM, said:

lennin59, 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;
}

```

### #4 brds

Reputation: 76
• 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;
}

```