Skip to content

Instantly share code, notes, and snippets.

@janogonzalez
Created December 11, 2012 16:50
Show Gist options
  • Save janogonzalez/4260212 to your computer and use it in GitHub Desktop.
Save janogonzalez/4260212 to your computer and use it in GitHub Desktop.
/**
* 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