Created
November 17, 2014 03:51
-
-
Save renoirb/4c79a1851e14347480c7 to your computer and use it in GitHub Desktop.
[add your bin description] // source http://jsbin.com/voziko
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="[add your bin description]" /> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// Fibonacci sequence handler | |
/** | |
* Fibonacci Math helper | |
* | |
* List first 14 terms (starting by 0): | |
* Fibonacci.fib(14) | |
* >> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] | |
* | |
* What is the n-th term? | |
* Fibonacci.term(6) | |
* >> 8 | |
* | |
* Ref: | |
* - https://www.math.hmc.edu/funfacts/ffiles/10002.4-5.shtml | |
* - http://www.askamathematician.com/2011/04/q-is-there-a-formula-to-find-the-nth-term-in-the-fibonacci-sequence/ | |
**/ | |
var Fibonacci = (function(){ | |
var GOLDEN_RATIO = (1 + Math.sqrt(5))/2, | |
GOLDEN_RATIO_NEG = (-1/fPhi(1)); | |
// should be the same: | |
// (1 + Math.sqrt(5))/2 | |
// (1/2)*(1 + Math.sqrt(5)) | |
function fPhi(exponent) { | |
return Math.pow(GOLDEN_RATIO, exponent); | |
} | |
// fPhi^n - (-1/fPhi) | |
function makeGoldenMean(position) { | |
var pos = parseInt(position); | |
return fPhi(pos) - Math.floor(-1/fPhi(pos)); | |
} | |
function genSeq(position) { | |
var list = [0,1], | |
pos = parseInt(position); | |
if(pos === 0) { | |
return list.slice(0,1); | |
} | |
if(pos === 1) { | |
return list; | |
} | |
pos = pos - 1; | |
for(i=0,j=1,k=0; k<pos; i=j, j=x, k++ ) { | |
x = i + j; | |
list.push(x); | |
} | |
return list; | |
} | |
function findTerm(position) { | |
return Math.floor(makeGoldenMean(position)/Math.sqrt(5)); | |
} | |
function proximity(nthTerm) { | |
var out = {nth:null, pos:null, previous:[]}; | |
out.nth = findTerm(nthTerm); | |
out.pos = parseInt(nthTerm); | |
out.previous.push(findTerm(nthTerm - 1)); | |
out.previous.push(findTerm(nthTerm - 2)); | |
return out; | |
} | |
var obj = {}; | |
obj.term = function(input){ return findTerm(input); }; | |
obj.fib = function(input){ return genSeq(input); }; | |
obj.previous = function(input){ return proximity(input); }; | |
return obj; | |
})(); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// Fibonacci sequence handler | |
/** | |
* Fibonacci Math helper | |
* | |
* List first 14 terms (starting by 0): | |
* Fibonacci.fib(14) | |
* >> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] | |
* | |
* What is the n-th term? | |
* Fibonacci.term(6) | |
* >> 8 | |
* | |
* Ref: | |
* - https://www.math.hmc.edu/funfacts/ffiles/10002.4-5.shtml | |
* - http://www.askamathematician.com/2011/04/q-is-there-a-formula-to-find-the-nth-term-in-the-fibonacci-sequence/ | |
**/ | |
var Fibonacci = (function(){ | |
var GOLDEN_RATIO = (1 + Math.sqrt(5))/2, | |
GOLDEN_RATIO_NEG = (-1/fPhi(1)); | |
// should be the same: | |
// (1 + Math.sqrt(5))/2 | |
// (1/2)*(1 + Math.sqrt(5)) | |
function fPhi(exponent) { | |
return Math.pow(GOLDEN_RATIO, exponent); | |
} | |
// fPhi^n - (-1/fPhi) | |
function makeGoldenMean(position) { | |
var pos = parseInt(position); | |
return fPhi(pos) - Math.floor(-1/fPhi(pos)); | |
} | |
function genSeq(position) { | |
var list = [0,1], | |
pos = parseInt(position); | |
if(pos === 0) { | |
return list.slice(0,1); | |
} | |
if(pos === 1) { | |
return list; | |
} | |
pos = pos - 1; | |
for(i=0,j=1,k=0; k<pos; i=j, j=x, k++ ) { | |
x = i + j; | |
list.push(x); | |
} | |
return list; | |
} | |
function findTerm(position) { | |
return Math.floor(makeGoldenMean(position)/Math.sqrt(5)); | |
} | |
function proximity(nthTerm) { | |
var out = {nth:null, pos:null, previous:[]}; | |
out.nth = findTerm(nthTerm); | |
out.pos = parseInt(nthTerm); | |
out.previous.push(findTerm(nthTerm - 1)); | |
out.previous.push(findTerm(nthTerm - 2)); | |
return out; | |
} | |
var obj = {}; | |
obj.term = function(input){ return findTerm(input); }; | |
obj.fib = function(input){ return genSeq(input); }; | |
obj.previous = function(input){ return proximity(input); }; | |
return obj; | |
})();</script></body> | |
</html> |
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
// Fibonacci sequence handler | |
/** | |
* Fibonacci Math helper | |
* | |
* List first 14 terms (starting by 0): | |
* Fibonacci.fib(14) | |
* >> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] | |
* | |
* What is the n-th term? | |
* Fibonacci.term(6) | |
* >> 8 | |
* | |
* Ref: | |
* - https://www.math.hmc.edu/funfacts/ffiles/10002.4-5.shtml | |
* - http://www.askamathematician.com/2011/04/q-is-there-a-formula-to-find-the-nth-term-in-the-fibonacci-sequence/ | |
**/ | |
var Fibonacci = (function(){ | |
var GOLDEN_RATIO = (1 + Math.sqrt(5))/2, | |
GOLDEN_RATIO_NEG = (-1/fPhi(1)); | |
// should be the same: | |
// (1 + Math.sqrt(5))/2 | |
// (1/2)*(1 + Math.sqrt(5)) | |
function fPhi(exponent) { | |
return Math.pow(GOLDEN_RATIO, exponent); | |
} | |
// fPhi^n - (-1/fPhi) | |
function makeGoldenMean(position) { | |
var pos = parseInt(position); | |
return fPhi(pos) - Math.floor(-1/fPhi(pos)); | |
} | |
function genSeq(position) { | |
var list = [0,1], | |
pos = parseInt(position); | |
if(pos === 0) { | |
return list.slice(0,1); | |
} | |
if(pos === 1) { | |
return list; | |
} | |
pos = pos - 1; | |
for(i=0,j=1,k=0; k<pos; i=j, j=x, k++ ) { | |
x = i + j; | |
list.push(x); | |
} | |
return list; | |
} | |
function findTerm(position) { | |
return Math.floor(makeGoldenMean(position)/Math.sqrt(5)); | |
} | |
function proximity(nthTerm) { | |
var out = {nth:null, pos:null, previous:[]}; | |
out.nth = findTerm(nthTerm); | |
out.pos = parseInt(nthTerm); | |
out.previous.push(findTerm(nthTerm - 1)); | |
out.previous.push(findTerm(nthTerm - 2)); | |
return out; | |
} | |
var obj = {}; | |
obj.term = function(input){ return findTerm(input); }; | |
obj.fib = function(input){ return genSeq(input); }; | |
obj.previous = function(input){ return proximity(input); }; | |
return obj; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment