Last active
September 2, 2020 09:49
-
-
Save sergiosvieira/8ab539677d18d5ad3ab52b74493abb0c to your computer and use it in GitHub Desktop.
Fast Fibonacci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <cmath> | |
int main() { | |
int input = 0; | |
std::ios::sync_with_stdio(false); | |
std::cin.tie(nullptr); | |
if (std::cin >> input) { | |
double sqrt5 = std::sqrt(5); | |
double left = (1. + sqrt5) / 2.; | |
double right = (1. - sqrt5) / 2.; | |
std::cout << std::fixed << std::setprecision(1) | |
<< (std::pow(left, input) - std::pow(right, input)) / sqrt5 | |
<< "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment