Skip to content

Instantly share code, notes, and snippets.

@salvianoo
Created May 20, 2012 14:29
Show Gist options
  • Save salvianoo/2758309 to your computer and use it in GitHub Desktop.
Save salvianoo/2758309 to your computer and use it in GitHub Desktop.
Fibonacci n4
#include <iostream>
using namespace std;
class fibonacci {
public:
static int calc_fibonacci(int n) {
int f, f1, f2;
if (n <= 2) {
return 1;
}
f1 = 1;
f2 = 1;
for (int i = 3; i <= n; i++) {
f = f1 + f2;
f1 = f2;
f2 = f;
}
return f;
}
};
int main (int argc, char * const argv[]) {
for (int n = 1; n <= 30; n++) {
cout << fibonacci::calc_fibonacci(n) <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment