Skip to content

Instantly share code, notes, and snippets.

@sergiosvieira
Last active September 2, 2020 09:49
Show Gist options
  • Save sergiosvieira/8ab539677d18d5ad3ab52b74493abb0c to your computer and use it in GitHub Desktop.
Save sergiosvieira/8ab539677d18d5ad3ab52b74493abb0c to your computer and use it in GitHub Desktop.
Fast Fibonacci
#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