Skip to content

Instantly share code, notes, and snippets.

@renoirb
Created November 17, 2014 05:25
Show Gist options
  • Save renoirb/689ca2bd1ed176729050 to your computer and use it in GitHub Desktop.
Save renoirb/689ca2bd1ed176729050 to your computer and use it in GitHub Desktop.
[Fibonacci sequence]// source http://jsbin.com/voziko
<!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 Math helper
*
* List first 14 terms (starting by 0):
* Fibonacci.fib(16)
* >> [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/
**/
/**
* Fivonacci JavaScript Entity DTO
*
* var a = new Fibonacci(5);
* console.log(a);
* >> [0, 1, 1, 2, 3, 5, 8]
**/
function Fibonacci(n) {
this.cb = Math2.fibonacci.create.bind(this);
this.cb.call(this, n);
delete this.cb;
return this.seq;
}
var Math2 = (function(){
var obj = {};
obj.GOLDEN_RATIO = (1 + Math.sqrt(5))/2;
obj.GOLDEN_RATIO_NEG = (-1/fPhi(1));
obj.fibonacci = {};
// should be the same:
// (1 + Math.sqrt(5))/2
// (1/2)*(1 + Math.sqrt(5))
function fPhi(exponent) {
return Math.pow(obj.GOLDEN_RATIO, exponent);
}
// fPhi^n - (-1/fPhi)
function makeGoldenMean(position) {
var pos = parseInt(position);
return fPhi(pos) - Math.floor(-1/fPhi(pos));
}
function findTerm(position) {
return Math.floor(makeGoldenMean(position)/Math.sqrt(5));
}
obj.fibonacci.term = findTerm;
obj.fibonacci.create = function fib(n, undefined){
if(!this.seq) {
this.seq = [0,1,1];
}
if(this.seq[n] === undefined) {
this.seq[n] = this.cb(n-1) + this.cb(n-2);
}
return this.seq[n];
}
return obj;
})();
</script>
<script id="jsbin-source-javascript" type="text/javascript">
/**
* Fibonacci Math helper
*
* List first 14 terms (starting by 0):
* Fibonacci.fib(16)
* >> [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/
**/
/**
* Fivonacci JavaScript Entity DTO
*
* var a = new Fibonacci(5);
* console.log(a);
* >> [0, 1, 1, 2, 3, 5, 8]
**/
function Fibonacci(n) {
this.cb = Math2.fibonacci.create.bind(this);
this.cb.call(this, n);
delete this.cb;
return this.seq;
}
var Math2 = (function(){
var obj = {};
obj.GOLDEN_RATIO = (1 + Math.sqrt(5))/2;
obj.GOLDEN_RATIO_NEG = (-1/fPhi(1));
obj.fibonacci = {};
// should be the same:
// (1 + Math.sqrt(5))/2
// (1/2)*(1 + Math.sqrt(5))
function fPhi(exponent) {
return Math.pow(obj.GOLDEN_RATIO, exponent);
}
// fPhi^n - (-1/fPhi)
function makeGoldenMean(position) {
var pos = parseInt(position);
return fPhi(pos) - Math.floor(-1/fPhi(pos));
}
function findTerm(position) {
return Math.floor(makeGoldenMean(position)/Math.sqrt(5));
}
obj.fibonacci.term = findTerm;
obj.fibonacci.create = function fib(n, undefined){
if(!this.seq) {
this.seq = [0,1,1];
}
if(this.seq[n] === undefined) {
this.seq[n] = this.cb(n-1) + this.cb(n-2);
}
return this.seq[n];
}
return obj;
})();</script></body>
</html>
/**
* Fibonacci Math helper
*
* List first 14 terms (starting by 0):
* Fibonacci.fib(16)
* >> [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/
**/
/**
* Fivonacci JavaScript Entity DTO
*
* var a = new Fibonacci(5);
* console.log(a);
* >> [0, 1, 1, 2, 3, 5, 8]
**/
function Fibonacci(n) {
this.cb = Math2.fibonacci.create.bind(this);
this.cb.call(this, n);
delete this.cb;
return this.seq;
}
var Math2 = (function(){
var obj = {};
obj.GOLDEN_RATIO = (1 + Math.sqrt(5))/2;
obj.GOLDEN_RATIO_NEG = (-1/fPhi(1));
obj.fibonacci = {};
// should be the same:
// (1 + Math.sqrt(5))/2
// (1/2)*(1 + Math.sqrt(5))
function fPhi(exponent) {
return Math.pow(obj.GOLDEN_RATIO, exponent);
}
// fPhi^n - (-1/fPhi)
function makeGoldenMean(position) {
var pos = parseInt(position);
return fPhi(pos) - Math.floor(-1/fPhi(pos));
}
function findTerm(position) {
return Math.floor(makeGoldenMean(position)/Math.sqrt(5));
}
obj.fibonacci.term = findTerm;
obj.fibonacci.create = function fib(n, undefined){
if(!this.seq) {
this.seq = [0,1,1];
}
if(this.seq[n] === undefined) {
this.seq[n] = this.cb(n-1) + this.cb(n-2);
}
return this.seq[n];
}
return obj;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment