Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. /* Author Name: Mohamed Elshenawy
  2. * Last Edit 28th July 2016
  3. * fast fibonacci calculation
  4. * This is the solution for Coursera Algorithm & Data sturcter course , Assignment for week 2 problem 2.
  5. */
  6.  
  7. #include <iostream>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. int main(int argc, char const *argv[])
  12. {
  13. unsigned long long fibIndex ,result;
  14. std::vector<unsigned long long > vfibbo;
  15.  
  16. cin >> fibIndex;
  17.  
  18. if (fibIndex <= 1)
  19. {
  20. result = fibIndex;
  21. }else
  22. {
  23. vfibbo.push_back(0);
  24. vfibbo.push_back(1);
  25. for (unsigned int i = 0; i <= fibIndex-2; i++)
  26. {
  27. unsigned long long tmp = ( vfibbo.at(i) % 10 + vfibbo.at(i+1) % 10 );
  28. vfibbo.push_back(tmp);
  29. }
  30.  
  31. result = vfibbo.back();
  32. }
  33. cout << result << endl;
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement