Created
April 21, 2018 23:25
-
-
Save SharanSMenon/7a16ddbe507f35ad4f8c6ba768684b89 to your computer and use it in GitHub Desktop.
This is a fibonacci number thing. It is a extremely efficient algorithm
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
class Fibonacci { | |
/** | |
* Fibonacci class | |
* A collection of functions | |
*/ | |
constructor() { | |
/** | |
* No variables for this constructor | |
*/ | |
console.log("Welcome to the Fibonacci class"); | |
console.log( | |
"This contains a set of functions that has to do with fibonacci numbers" | |
); | |
console.log("There are also basic functions like add and subtract"); | |
} | |
fib(n) { | |
if (n == 0) { | |
return [0, 1]; | |
} else { | |
var a = this.fib(Math.floor(n / 2))[0]; | |
var b = this.fib(Math.floor(n / 2))[1]; | |
var c = a * (b * 2 - a); | |
var d = a * a + b * b; | |
if (n % 2 == 0) { | |
return [c, d]; | |
} else { | |
return [d, c + d]; | |
} | |
} | |
} | |
calcFib(n) { | |
/** | |
* Calculates the nth fibonacci number | |
*/ | |
if (n < 0) { | |
return "Negative arguments not supported"; | |
} | |
return this.fib(n)[0] | |
} | |
fibonacciList(n){ | |
/** | |
* Returns a list of fibonacci numbers from 1 to n | |
*/ | |
var returnList = []; | |
for (let i = 0; i < n + 1; i++) { | |
returnList.push(this.calcFib(i)); | |
} | |
return returnList; | |
} | |
add(a, b){ | |
return a + b; | |
} | |
} | |
fib = new Fibonacci() | |
console.log(fib.calcFib(10)); | |
console.log(fib.fibonacciList(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment