The best Fibonacci is achieved in js
The best Fibonacci is achieved in js
the best realized by using js 斐波那契数列
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-12-10
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
// good O(2*n)
function fibonacci(num, memo) {
memo = memo || {};
if (memo[num]) {
return memo[num];
}
if (num <= 1) {
return 1;
}
return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo);
}
// bad O(2^n), 卡死了
function fib(n) {
// 1, 1, 2, 3, 5, 8...n, (n - 1) + (n - 2)
if(n <= 2) {
return 1;
} else {
// n >= 3
return fib(n - 1) + fib(n - 2);
}
}
function f(num, memo) {
memo = memo || {};
if (memo[num]) {
return memo[num];
}
if (num <= 2) {
return 1;
}
// RangeError: Maximum call stack size exceeded ???
return memo[num] = f(num - 1, memo) + f(num - 2, memo);
}
function sumOddFibonacciNumbers(num) {
// write code here.
let result = 0;
for (let i = 1; i <= num; i++) {
const temp = f(i);
// const temp = fibonacci(i);
// const temp = fib(i);
if(temp <= num && (temp % 2) !== 0) {
// odd
result += temp;
// log(`temp =`, temp);
}
}
return result;
}
const test1 = sumOddFibonacciNumbers(10);
// 10
const test2 = sumOddFibonacciNumbers(1000);
// 1785
// const test3 = sumOddFibonacciNumbers(4000000);
// 4613732
log(`\ntest =`, test1, test1 === 10 ? `` : ``);
log(`\ntest =`, test2, test2 === 1785 ? `` : ``);
// log(`\ntest =`, test3, test3 === 4613732 ? `` : ``);
// RangeError: Maximum call stack size exceeded ???
bug
function fibonacci(num, memo) {
memo = memo || {};
if (memo[num]) return memo[num];
if (num <= 1) return 1;
return memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo);
}
fibonacci(4000000);

// 在 ES6 规范中,有一个尾调用优化,可以实现高效的尾递归方案。
// ES6 的尾调用优化只在严格模式下开启,正常模式是无效的。
'use strict'
function fib(n, current = 0, next = 1) {
if(n == 0) return 0;
if(n == 1) return next;
return fib(n - 1, next, current + next);
}
fib(4000000);
// Uncaught RangeError: Maximum call stack size exceeded

refs
https://www.cnblogs.com/xgqfrms/p/12909516.html
https://www.cnblogs.com/xgqfrms/archive/2004/01/13/12909516.html
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
The best Fibonacci is achieved in js的更多相关文章
- Function.prototype.toString 的使用技巧
Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...
- 荷畔微风 - 在函数计算FunctionCompute中使用WebAssembly
WebAssembly 是一种新的W3C规范,无需插件可以在所有现代浏览器中实现近乎原生代码的性能.同时由于 WebAssembly 运行在轻量级的沙箱虚拟机上,在安全.可移植性上比原生进程更加具备优 ...
- https://www.jianshu.com/writer#/notebooks/164311/notes/88906048/preview
什么是 webassembly 在 2019 年 12 月之前,如果你要编写一个web页面,那一定离不开 html.css.js 这三个好兄弟.在 2019 年 12 月之后 W3C 宣布 webas ...
- 算法——js(Fibonacci数列)
斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家列昂纳多·斐波那契(Leonardoda Fibonacci[1] )以兔子繁殖为例子而引入,故又称为“兔子数列”,指 ...
- 关于fibonacci数列用JS写的一点小优化
直接上代码 var month = prompt("请输入月数:") function fibobo(x) { //先定义一个已有前两项的数组,用来作缓存 var arr = [1 ...
- Understanding Asynchronous IO With Python 3.4's Asyncio And Node.js
[转自]http://sahandsaba.com/understanding-asyncio-node-js-python-3-4.html Introduction I spent this su ...
- Node.js:进程、子进程与cluster多核处理模块
1.process对象 process对象就是处理与进程相关信息的全局对象,不需要require引用,且是EventEmitter的实例. 获取进程信息 process对象提供了很多的API来获取当前 ...
- 深入解析js异步编程利器Generator
我们在编写Nodejs程序时,经常会用到回调函数,在一个操作执行完成之后对返回的数据进行处理,我简单的理解它为异步编程. 如果操作很多,那么回调的嵌套就会必不可少,那么如果操作非常多,那么回调的嵌套就 ...
- Underscore.js
概述 Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程.MVC框架Backbone.js就将这个库 ...
随机推荐
- CMU数据库(15-445)实验2-B+树索引实现(下+课上笔记)
4. Index_Iterator实现 这里就是需要实现迭代器的一些操作,比如begin.end.isend等等 下面是对于IndexIterator的构造函数 template <typena ...
- Connections could not be acquired from the underlying database!
Connections could not be acquired from the underlying database! 报错截图: 报错内容: Exception in thread &quo ...
- (006)每日SQL学习:关于to_char函数
to_char函数的官方文档说明: 详细to_char请移步:https://www.cnblogs.com/reborter/archive/2008/11/28/1343195.html 需求:n ...
- 获取当前文件路径 import 原理 一般把模块组成的集合称为包(package)
获取当前文件路径 testpath.py import sysprint(sys.path) [root@d mapReduceLog]# python testpath.py['/data/mapR ...
- chmod a+w . 权限控制 su、sudo 修改文件所有者和文件所在组 添加用户到sudoer列表中 当前用户信息
对当前目录对所有用户开放读写权限 chmod a+r . $ sudo chmod -R a+w /usr/lib/python2.7 所有用户添加文件的写权限 [linux]su.sudo.sudo ...
- Spring Maven配置
看的似懂非懂 https://www.cnblogs.com/webyyq/p/8799727.html https://blog.csdn.net/l00149133/article/details ...
- 十:SpringBoot-配置AOP切面编程,解决日志记录业务
SpringBoot-配置AOP切面编程,解决日志记录业务 1.AOP切面编程 1.1 AOP编程特点 1.2 AOP中术语和图解 2.SpringBoot整合AOP 2.1 核心依赖 2.2 编写日 ...
- ThinkPHP 漏洞利用
ThinkPHP thinkphp_5x_命令执行漏洞 受影响版本包括5.0和5.1版本 docker漏洞环境源码: https://github.com/vulnspy/thinkphp-5.1.2 ...
- toggle() 隐藏和收缩
<!DOCTYPE html><html><head><script src="/jquery/jquery-1.11.1.min.js" ...
- 操作系统中的描述符和GDT
在操作系统中,全局描述符是什么?GDT又是什么?在进入保护模式之前,准备好GDT和GDT中的描述符是必须的吗?用汇编代码怎么创建描述符?本文解答上面几个问题. 在实模式下,CPU是16位的,意思是,寄 ...