Created
December 11, 2012 16:50
-
-
Save janogonzalez/4260212 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* El mítico "golden ratio" | |
*/ | |
var PHI = (1 + Math.sqrt(5)) / 2; | |
/** | |
* Logaritmo con base arbitraria. | |
*/ | |
function log(n, base) { | |
return Math.log(n) / Math.log(base); | |
} | |
/** | |
* Busca el fibonacci más cercano (de forma aproximada) de acuerdo a | |
* http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding | |
*/ | |
function closestFibonacci(f) { | |
return Math.floor(log(f * Math.sqrt(5) + 0.5, PHI)); | |
} | |
/** | |
* Busca cuántos fibonacci hay en el rango [a, b] | |
*/ | |
function fibonacciBetween(a, b) { | |
return Math.abs(closestFibonacci(b) - closestFibonacci(a)); | |
} | |
// El ejemplo que aparecía en el desafío | |
console.log(fibonacciBetween(9876543210, 1234567890)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment