Skip to content

Instantly share code, notes, and snippets.

@pizzarob
Last active October 15, 2016 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pizzarob/eb2a72f0cd6bea2f9a3abb6c2c2c1de4 to your computer and use it in GitHub Desktop.
Save pizzarob/eb2a72f0cd6bea2f9a3abb6c2c2c1de4 to your computer and use it in GitHub Desktop.
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying F(n) * F(n+1) = prod.
// Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying F(n) * F(n+1) = prod
// My solution to the code wars challenge https://www.codewars.com/kata/5541f58a944b85ce6d00006a/train/javascript
// Not used for this, but here's some code to test if a number is in the Fibonacci sequence.
// const isPerfectSquare = num => {
// const sq = Math.sqrt(num);
// return sq * sq === num;
// }
// const isFib = num => {
// return isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4);
// }
const phi = (Math.sqrt(5) + 1) / 2;
const getF = num => {
return Math.round(((Math.pow(phi, num)) - (-Math.pow(phi, -num))) / Math.sqrt(5))
}
const testProduct = (f) => {
return getF(f) * getF(f + 1);
}
const productFib = prod => {
let fib = 0;
let candidate = 0;
let match = false;
while(candidate < prod + 1){
fib += 1;
candidate = testProduct(fib);
if(candidate === prod){
match = true;
break;
}
}
return [getF(fib), getF(fib + 1), match];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment