Skip to main content

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var SQRT_5 = Math.sqrt(5); var PHI = (SQRT_5 + 1) / 2; var yourself = { fibonacci : function(n) { if (n ===<= 01) { return 0;n; } if (n === 1) {  return 1;  } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var SQRT_5 = Math.sqrt(5); var PHI = (SQRT_5 + 1) / 2; var yourself = { fibonacci : function(n) { if (n === 0) { return 0; } if (n === 1) {  return 1;  } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var SQRT_5 = Math.sqrt(5); var PHI = (SQRT_5 + 1) / 2; var yourself = { fibonacci : function(n) { if (n <= 1) { return n; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 
fixed syntax; variables are now global
Source Link
SirPython
  • 13.5k
  • 3
  • 38
  • 93

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var yourself = { var SQRT_5 = Math.sqrt(5);  var PHI = (SQRT_5 + 1) / 2; var yourself = {  fibonacci : function(n) {   if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var yourself = { var SQRT_5 = Math.sqrt(5);  var PHI = (SQRT_5 + 1) / 2; fibonacci : function(n) {   if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var SQRT_5 = Math.sqrt(5); var PHI = (SQRT_5 + 1) / 2; var yourself = {  fibonacci : function(n) { if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 
deleted 10 characters in body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var yourself = { fibonacci : function(n) { constvar SQRT_5 = Math.sqrt(5);   constvar PHI = (SQRT_5 + 1) / 2; fibonacci : function(n) { if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var yourself = { fibonacci : function(n) { const SQRT_5 = Math.sqrt(5);   const PHI = (SQRT_5 + 1) / 2; if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 

If you are just looking for a simple function that gets the nth Fibonacci number, you can use phi:

$$\frac{\sqrt5 + 1}2$$

To calculate the nth fibonacci number:

$$fib(n)=round(\phi^n/\sqrt5)$$

I don't know javascript very well, but if you can store the value of phi and square root of 5, you can do:

var yourself = { var SQRT_5 = Math.sqrt(5); var PHI = (SQRT_5 + 1) / 2; fibonacci : function(n) { if (n === 0) { return 0; } if (n === 1) { return 1; } return Math.round(Math.pow(PHI, n) / SQRT_5); } }; 
edited math functions into code
Source Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155
Loading
added 10 characters in body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96
Loading
added 4 characters in body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96
Loading
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96
Loading