当前位置:网站首页>JS implementation of Fibonacci sequence
JS implementation of Fibonacci sequence
2022-06-21 23:25:00 【samscat】
Fibonacci sequence :0、1、1、2、3、5、8、13、21、34、…… In Mathematics , The Fibonacci sequence is defined recursively as follows :F(0)=0,F(1)=1, F(n)=F(n - 1)+F(n - 2)(n ≥ 2,n ∈ N*)
// Use for loop
function fibonacci(n) {
let fibo = [1,1]
let a = 1;
let b = 1;
for (let index = 0; index < n; index++) {
[b, a] = [a, a+b]
fibo.push(a)
}
return fibo.toString()
}
console.log(fibonacci(6))
// 1,1,2,3,5,8,13,21
// Use while loop
function fibonacci2 (n) {
let nums = [1]
let count = 1;
let prev = 0;
let curr = 1;
if (n == 1) {
return [0]
}
while (count < n - 1) {
curr += prev;
prev = curr - prev;
nums.push(curr);
count ++;
}
nums.unshift(0);
return nums
}
console.log(fibonacci2(6));
// [0, 1, 1, 2, 3, 5]
// Using recursion
function fibonacci3(n) {
if (n < 2) return n
return fibonacci2(n-1) + fibonacci2(n-2)
}
let ff = []
for (let index = 0; index < 6; index++) {
ff.push(fibonacci3(index))
}
console.log(ff)
// [0,1,1,2,3,5]
边栏推荐
- How to associate the QR code of wechat applet and realize the integration of two codes
- Qt中常用的窗体
- 關於 麒麟系統開發錯誤“fatal error: GL/gl.h: No such file or directory“ 的解决方法
- WSL 2 installation process (and introduction)
- 2022-06-21:golang选择题,以下golang代码输出什么?A:3;B:4;C:100;D:编译失败。 package main import (
- 2022年云计算的趋势有哪些?
- mvn deploy多个模块的bat文件
- Cola and herbal tea speed up mutual rolling
- Native applet application applet - publishing process
- Prediction of enzyme activity parameters by deep learning construction of enzyme binding model
猜你喜欢

Multi order MFCC extraction for speech signal processing (matlab)

關於 麒麟系統開發錯誤“fatal error: GL/gl.h: No such file or directory“ 的解决方法

danfoss丹佛斯变频器维修VLT5000/VLT6000/VLT8000

Resolve the invalidation of OpenCV code prompt in pycharm

How to adjust the resolution of the computer screen? Computer screen modification resolution switchresx

Speech breakpoint detection (short time improved subband spectral entropy)

Professional font design editor glyphs 3

Getting to know Vxe table (I)

关于 SecureFx传输远程服务器中文显示乱码 的解决方法
![[use four tricky examples to help you understand] how data is stored in memory](/img/ef/372ae4483bda909318cbdc05bd38f0.png)
[use four tricky examples to help you understand] how data is stored in memory
随机推荐
PLT label effective
Some users of uniapp wechat authorization cannot be authorized normally
MySQL pit records
Native applet application applet - publishing process
WSL 2 installation process (and introduction)
Qt文档阅读笔记-staticMetaObject解析与实例
微信小程序获取网络状态
uniapp版本更新 热更新及自然更新
Project change management
Hardware development notes (IV): basic process of hardware development, making a USB to RS232 module (III): design schematic diagram
Qt中常用的窗体
[wustctf2020] plain and unpretentious -1
Parallel search exercise 1: circle of friends
Resolve the invalidation of OpenCV code prompt in pycharm
JUnit VS TestNG
关于 QtCreator的设计器QtDesigner完全无法正常拽托控件 的解决方法
Notes de développement de la tarte aux framboises (XVI): la tarte aux framboises 4b + installe la base de données mariadb (MySQL open source Branch) et teste les opérations de base
About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started
keep-alive的使用注意点
关于 SecureFx传输远程服务器中文显示乱码 的解决方法