Skip to content

Added fibonacci.cpp #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions fibonacci.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <iostream>
#include <cassert>
#define ll long long
// The following code calls a naive algorithm for computing a Fibonacci number.
//
// What to do:
// 1. Compile the following code and run it on an input "40" to check that it is slow.
// You may also want to submit it to the grader to ensure that it gets the "time limit exceeded" message.
// 2. Implement the fibonacci_fast procedure.
// 3. Remove the line that prints the result of the naive algorithm, comment the lines reading the input,
// uncomment the line with a call to test_solution, compile the program, and run it.
// This will ensure that your efficient algorithm returns the same as the naive one for small values of n.
// 4. If test_solution() reveals a bug in your implementation, debug it, fix it, and repeat step 3.
// 5. Remove the call to test_solution, uncomment the line with a call to fibonacci_fast (and the lines reading the input),
// and submit it to the grader.

ll fibonacci_naive(ll n) {
if (n <= 1)
return n;

return fibonacci_naive(n - 1) + fibonacci_naive(n - 2);
}

ll fibonacci_fast(ll n) {
if (n <= 1)
return n;
ll first=0,second=1;
ll third;
for(ll i=2;i<=n;i++)
{
third=first+second;
first=second;
second=third;
}
return third;
}

void test_solution() {
assert(fibonacci_fast(3) == 2);
assert(fibonacci_fast(10) == 55);
for (int n = 0; n < 20; ++n)
assert(fibonacci_fast(n) == fibonacci_naive(n));
}

int main() {
ll n = 0;
std::cin >> n;

//std::cout << fibonacci_naive(n) << '\n';
//test_solution();
std::cout << fibonacci_fast(n) << '\n';
return 0;
}