Skip to content

Instantly share code, notes, and snippets.

@fernandosavio
Last active January 13, 2017 03:49
Show Gist options
  • Save fernandosavio/871e9f9acccdbedf9d0c to your computer and use it in GitHub Desktop.
Save fernandosavio/871e9f9acccdbedf9d0c to your computer and use it in GitHub Desktop.
Find the N-th fibonacci number using Binet's Formula.
// http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html
function fibonacci_nth(i){
var v5 = Math.sqrt(5),
Phi = ( v5 + 1 ) / 2,
phi = Phi-1;
return Math.round( Math.pow(Phi, i) / v5 - Math.pow(-phi, i) / v5 );
}
<?php
# http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html
function fibonacci_nth($i){
$v5 = sqrt(5);
$Phi = ( $v5 + 1 ) / 2;
$phi = $Phi-1;
return intval( round( pow($Phi, i) / $v5 - pow(-$phi, i) / $v5 ) );
}
import math
# http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html
def fibonacci_nth(i):
v5 = math.sqrt(5)
Phi = ( v5 + 1 ) / 2
phi = Phi-1
return int( round( (Phi ** i) / v5 - (-phi ** i) / v5 ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment