Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Author Name: Mohamed Elshenawy
- * Last Edit 28th July 2016
- * fast fibonacci calculation
- * This is the solution for Coursera Algorithm & Data sturcter course , Assignment for week 2 problem 2.
- */
- #include <iostream>
- #include <vector>
- using namespace std;
- int main(int argc, char const *argv[])
- {
- unsigned long long fibIndex ,result;
- std::vector<unsigned long long > vfibbo;
- cin >> fibIndex;
- if (fibIndex <= 1)
- {
- result = fibIndex;
- }else
- {
- vfibbo.push_back(0);
- vfibbo.push_back(1);
- for (unsigned int i = 0; i <= fibIndex-2; i++)
- {
- unsigned long long tmp = ( vfibbo.at(i) % 10 + vfibbo.at(i+1) % 10 );
- vfibbo.push_back(tmp);
- }
- result = vfibbo.back();
- }
- cout << result << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement