# Fibonacci challenge

• (3 Pages)
• 1
• 2
• 3

## 40 Replies - 33420 Views - Last Post: 25 April 2015 - 11:23 AM

### #31 p.mo

Reputation: 1
• Posts: 1
• Joined: 02-October 12

## Re: Fibonacci challenge

Posted 02 October 2012 - 07:58 PM

This is my first post and challenge. While its not very compact should be functional functional.

Spoiler

### #32 HB-aka

Reputation: 0
• Posts: 62
• Joined: 03-June 14

## Re: Fibonacci challenge

Posted 09 June 2014 - 03:02 AM

this function will get Fibonacci numbers.
i used "long integers" because the number can get to big.
so...this is the code i'v made.
Spoiler

This post has been edited by Skydiver: 09 June 2014 - 08:22 PM
Reason for edit:: Put code in spoiler tags to keep challenge fresh for late comers.

### #33 tifuy95

Reputation: 0
• Posts: 1
• Joined: 26-September 14

## Re: Fibonacci challenge

Posted 26 September 2014 - 11:20 AM

I am a new programer can someone give me feedback on my code??
```// Made by Max Bentata

//09/21/2014

#include <iostream>
using namespace std;

int main()
{
int x = 0;
int fib = 0;
int sum = 1;
int marker = 0;

cout << x << endl;
x = fib + sum;
cout << x << endl;
fib = x + sum;
cout << fib << endl;

x = fib + sum;
cout << x << endl;

while (mkr < 15){
sum = x + fib;
cout << sum << endl;
fib = sum + x;
cout << fib << endl;
x = fib + sum;
cout << x << endl;
marker++;
}

return 0;
}

```

### #34 compscistud

Reputation: 0
• Posts: 17
• Joined: 21-October 14

## Re: Fibonacci challenge

Posted 18 November 2014 - 01:20 AM

this was my assignment last week lol

### #35 vividexstance

• D.I.C Lover

Reputation: 743
• Posts: 2,597
• Joined: 31-December 10

## Re: Fibonacci challenge

Posted 18 November 2014 - 12:04 PM

tifuy95, on 26 September 2014 - 01:20 PM, said:

I am a new programer can someone give me feedback on my code??
```// Made by Max Bentata

//09/21/2014

#include <iostream>
using namespace std;

int main()
{
int x = 0;
int fib = 0;
int sum = 1;
int marker = 0;

cout << x << endl;
x = fib + sum;
cout << x << endl;
fib = x + sum;
cout << fib << endl;

x = fib + sum;
cout << x << endl;

while (mkr < 15){
sum = x + fib;
cout << sum << endl;
fib = sum + x;
cout << fib << endl;
x = fib + sum;
cout << x << endl;
marker++;
}

return 0;
}

```

One thing that stood out for me is that you use what are called "Magic Numbers", specifically, the while(marker < 15). What is that 15 supposed to represent? Would it be used more than once if this were a larger program? If so, what happens when you want to change the 15 to something else? You would have to go through the code and replace the number and hope that you don't miss any, or if you do, that it will be noticed and easily found.

The tip, would be to use a variable, specifically, a const variable that doesn't change throughout the program. This way, you can give the number a descriptive name which helps people reading the code to understand it a little easier. Also, it makes changing/maintaining the code easier because you only have to make the change in one place instead of many.

### #36 9644026902

Reputation: 0
• Posts: 1
• Joined: 27-November 14

## Re: Fibonacci challenge

Posted 27 November 2014 - 08:03 AM

Spoiler

This post has been edited by Skydiver: 22 January 2015 - 10:48 PM
Reason for edit:: Put code in spoiler tags.

### #37 fj_j_greenhalgh

Reputation: 0
• Posts: 1
• Joined: 05-December 14

## Re: Fibonacci challenge

Posted 05 December 2014 - 06:04 PM

Template Metaprogramming C++11

Spoiler

This post has been edited by Skydiver: 22 January 2015 - 10:47 PM
Reason for edit:: Put code in spoiler tags.

### #38 Skydiver

• Code herder

Reputation: 3928
• Posts: 12,468
• Joined: 05-May 12

## Re: Fibonacci challenge

Posted 22 January 2015 - 10:47 PM

fj_j_greenhalgh: See post #27.

### #39 its_MCD

Reputation: 0
• Posts: 1
• Joined: 21-February 15

## Re: Fibonacci challenge

Posted 21 February 2015 - 06:49 AM

I found a way to make a function for fibonacci's formula by using ading ect.
```int fibonacci(){
unsigned long long int nom1 = 0;
unsigned long long int nom2 = 1;

while(nom2 < 999999999){
printf("%d \n",nom1 + nom2);
int nom3 = nom1 + nom2;
printf("%d \n",nom2 + nom3);
nom1 = nom2 + nom3;
printf("%d \n",nom3 + nom1);
nom2 = nom3 + nom1;

}

return 0;}
```

its_MCD, on 21 February 2015 - 06:48 AM, said:

I found a way to make a function for fibonacci's formula by using ading ect.
```int fibonacci(){
unsigned long long int nom1 = 0;
unsigned long long int nom2 = 1;

while(nom2 < 999999999){
printf("%d \n",nom1 + nom2);
int nom3 = nom1 + nom2;
printf("%d \n",nom2 + nom3);
nom1 = nom2 + nom3;
printf("%d \n",nom3 + nom1);
nom2 = nom3 + nom1;

}

return 0;}
```
sorry i don't know how the code should come

### #40 disceresui

Reputation: 0
• Posts: 1
• Joined: 25-April 15

## Re: Fibonacci challenge

Posted 25 April 2015 - 10:50 AM

Reference: http://www.nayuki.io...acci-algorithms

I simply used the matrix exponentiation identity to achieve a runtime of O(n).
Also, the recursive version of the power function can reduce the runtime to O(logn).

Matrix Exponentiation
Spoiler

### #41 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11109
• Posts: 41,639
• Joined: 27-December 08

## Re: Fibonacci challenge

Posted 25 April 2015 - 11:23 AM

Solving the recurrence by hand, then using the closed form solution is actually faster than using the matrix multiplication method. A common approach to speed up matrix multiplication is to diagonalize the matrix and write it in terms of similar matrices. The diagonal matrix of eigenvalues are exactly the eigenvalues of the recurrence.

The matrix trick is still a neat one that many overlook! Good show with it.