Last active
January 13, 2017 03:49
-
-
Save fernandosavio/871e9f9acccdbedf9d0c to your computer and use it in GitHub Desktop.
Find the N-th fibonacci number using Binet's Formula.
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
// 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 ); | |
} |
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
<?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 ) ); | |
} |
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
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