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

https://scrimba.com/learn/adventcalendar/-javascript-challenge-sum-odd-fibonacci-numbers-introduction-cmpWaRcW



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


The best Fibonacci is achieved in js的更多相关文章

  1. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  2. 荷畔微风 - 在函数计算FunctionCompute中使用WebAssembly

    WebAssembly 是一种新的W3C规范,无需插件可以在所有现代浏览器中实现近乎原生代码的性能.同时由于 WebAssembly 运行在轻量级的沙箱虚拟机上,在安全.可移植性上比原生进程更加具备优 ...

  3. https://www.jianshu.com/writer#/notebooks/164311/notes/88906048/preview

    什么是 webassembly 在 2019 年 12 月之前,如果你要编写一个web页面,那一定离不开 html.css.js 这三个好兄弟.在 2019 年 12 月之后 W3C 宣布 webas ...

  4. 算法——js(Fibonacci数列)

    斐波那契数列(Fibonacci sequence),又称黄金分割数列,因数学家列昂纳多·斐波那契(Leonardoda Fibonacci[1]  )以兔子繁殖为例子而引入,故又称为“兔子数列”,指 ...

  5. 关于fibonacci数列用JS写的一点小优化

    直接上代码 var month = prompt("请输入月数:") function fibobo(x) { //先定义一个已有前两项的数组,用来作缓存 var arr = [1 ...

  6. 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 ...

  7. Node.js:进程、子进程与cluster多核处理模块

    1.process对象 process对象就是处理与进程相关信息的全局对象,不需要require引用,且是EventEmitter的实例. 获取进程信息 process对象提供了很多的API来获取当前 ...

  8. 深入解析js异步编程利器Generator

    我们在编写Nodejs程序时,经常会用到回调函数,在一个操作执行完成之后对返回的数据进行处理,我简单的理解它为异步编程. 如果操作很多,那么回调的嵌套就会必不可少,那么如果操作非常多,那么回调的嵌套就 ...

  9. Underscore.js

    概述 Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程.MVC框架Backbone.js就将这个库 ...

随机推荐

  1. C语言中二维数组声明时,探究省略第一维的原因

    我们在使用二维数组作为参数时,我们既可以指明这个数组各个维度的维数,同时我们也可以省略一维,但是二维却不能省略.why呢?由于编译器原理的限制,在一个数组Elemtype test[m][n]中,访问 ...

  2. 导出带有图片的excel

    public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("d:\ ...

  3. Codeforces 1437F Emotional Fishermen(思维,dp)

    题意 给出数列\(a_i\),求排列\(p_i\)的数量满足 \[\frac{a_{p_i}}{max_{j=1}^{i-1}a_{p_j}} \notin (\frac{1}{2},2) \] 思路 ...

  4. Python 学习笔记(1)

    Mac下载安装Python mac 系统自带有python .但就最新的mac系统而言,它自带的python版本为2.*版本. 虽然不影响对于老python项目的运行,但3.*版本中很多语法都发生了改 ...

  5. cookie机制、session机制

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  6. JVM虚拟机Class类文件研究分析

    前言 为了研究Class文件,先编写一个最简单的代码: package com.courage; public class T0100_ByteCode01 { } 之所以说最简单,是因为这个类里面任 ...

  7. Spring Boot 核心配置文件 bootstrap & application

    Spring Boot 核心配置文件 bootstrap & application 1.SpringBoot bootstrap配置文件不生效问题 2.bootstrap/ applicat ...

  8. Docker安装Mycat并实现mysql读写分离,分库分表

    Docker安装Mycat并实现mysql读写分离,分库分表 一.拉取mycat镜像 二.准备挂载的配置文件 2.1 创建文件夹并添加配置文件 2.1.1 server.xml 2.1.2 serve ...

  9. linux shell判断文件,目录是否存在或者具有权限

    在linux中判断文件,目录是否存在或则具有的权限,根据最近的学习以及网上的资料,进行了以下的总结: #!/bin/sh myPath="/var/log/httpd/" myFi ...

  10. shell(shell函数、shell正则表达式)

    本章内容 shell函数 shell正则表达式 1.shell函数 linux shell 可以用户定义函数,然后在shell脚本中可以随便调用. 格式: funname () { CMD #函数体 ...