Languages
[Edit]
EN

JavaScript - Iterative Fibonacci sequence / number algorithm

13 points
Created by:
Root-ssh
164090

Using JavaScript it is possible to compute the Fibonacci sequence in the following way.

Iterative algorithm example

// ONLINE-RUNNER:browser;

function calculateValue(number) {
  	if(number < 1) {
    	return 0;
    }
    var a = 0;
    var b = 1;
    for (var i = 1; i < number; ++i) {
		var c = a + b;
      	a = b;
      	b = c;
    }
    return b;
}


// Example:

for (var n = 0; n < 10; ++n) {
    console.log('f(' + n + ')=' + calculateValue(n));
}

Output:

f(0)=0
f(1)=1
f(2)=1
f(3)=2
f(4)=3
f(5)=5
f(6)=8
f(7)=13
f(8)=21
f(9)=34
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - math (popular problems)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join