fibonacci number & fibonacci sequence
fibonacci number & fibonacci sequence
https://www.mathsisfun.com/numbers/fibonacci-sequence.html
http://www.shuxuele.com/numbers/fibonacci-sequence.html
fibonacci sequence with cache
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-05-17
* @modified
*
* @description fibonacci 算法缓存优化 javascript
* @augments
* @example
* @link https://en.wikipedia.org/wiki/Fibonacci_number
*
*/
/*
for n = 0 and n = 1, Fn = F(n)
F0 = 0
F1 = 1
for n > 1, Fn = F(n - 1) + F(n - 2);
F2 = F1 + F0 = 1
F3 = F2 + F1 = 2
F4 = F3 + F2 = 3
F5 = F4 + F3 = 5
...
The beginning of the fibonacci sequence is thus:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
*/
const log = console.log;
const fib = (n) => {
const caches = [0, 1];
if(n > 1 && typeof caches[n] === "undefined") {
const temp = fib(n - 2) + fib(n - 1);
caches[n] = temp;
}
// log(`caches[n]`, n, caches[n])
return caches[n];
}
// const fib = (n) => {
// const caches = [0, 1];
// if(n === 0 || n === 1) {
// return caches[n];
// } else {
// if(typeof caches[n] === "undefined") {
// const temp = fib(n - 2) + fib(n - 1);
// caches[n] = temp;
// }
// // log(`caches[n]`, n, caches[n])
// return caches[n];
// }
// }
// const fib = (n) => {
// const caches = [0, 1];
// if(n === 0 || n === 1) {
// return caches[n];
// } else {
// // log(`caches[n]`, n, caches[n])
// if(typeof caches[n] !== "undefined") {
// return caches[n];
// } else {
// const temp = fib(n- 2) + fib(n- 1);
// caches[n] = temp;
// return temp;
// }
// }
// }
const v0 = fib(0)
log(`fib0`, v0)
const v1 = fib(1)
log(`fib1`, v1)
const v3 = fib(3)
log(`fib3`, v3)
const v5 = fib(5)
log(`fib5`, v5)
Fibonacci memory 缓存优化
// USING MEMOIZATION
function fibonacci(n,memo) {
memo = memo || {}
if (memo[n]) {
return memo[n]
}
if (n <= 1) {
return 1
}
return memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo)
}
Fibonacci languages race rank sorter
hackster
https://www.hackster.io/users/preferences?show_welcome=true
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
fibonacci number & fibonacci sequence的更多相关文章
- 求四百万以内Fibonacci(number)数列偶数结果的总和
又对啦...开心~~~~ 只是代码可能不符合PEP标准什么的... Each new term in the Fibonacci sequence is generated by adding the ...
- 【LEETCODE】44、509. Fibonacci Number
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- Buge's Fibonacci Number Problem
Buge's Fibonacci Number Problem Description snowingsea is having Buge’s discrete mathematics lesson, ...
- [UCSD白板题] The Last Digit of a Large Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- [UCSD白板题 ]Small Fibonacci Number
Problem Introduction The Fibonacci numbers are defined as follows: \(F_0=0\), \(F_1=1\),and \(F_i=F_ ...
- (斐波那契总结)Write a method to generate the nth Fibonacci number (CC150 8.1)
根据CC150的解决方式和Introduction to Java programming总结: 使用了两种方式,递归和迭代 CC150提供的代码比较简洁,不过某些细节需要分析. 现在直接运行代码,输 ...
- Fibonacci number
https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/master/Moc ...
- Algorithms - Fibonacci Number
斐波那契数列(Fibonacci Number)从数学的角度是以递归的方法定义的: \(F_0 = 0\) \(F_1 = 1\) \(F_n = F_{n-1} + F_{n-2}\) (\(n \ ...
- 【leetcode】509. Fibonacci Number
problem 509. Fibonacci Number solution1: 递归调用 class Solution { public: int fib(int N) { ) return N; ...
随机推荐
- PC端微信多开方式.bat(Windows批处理文件)
start 微信安装路径\WeChat.exe start 微信安装路径\WeChat.exe
- 下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格。
package org.apache.hadoop.examples; import java.util.HashMap; import java.io.IOException; import jav ...
- (001)每日SQL学习:关于UNION的使用
union内部必须有相同的列或者相同的数据类型,同时,每条 SELECT 语句中的列的顺序必须相同.union合并了select的结果集. union 与union all的不同: union合并了重 ...
- Java泛型机制
泛型程序设计 1.泛型程序设计的起源? 泛型是JDK1.5增加的新特性. 2.使用泛型的好处? 使用泛型机制编写的代码比那些杂乱使用Object变量,然后再进行强制类型转换的代码具有更好的安全性和可读 ...
- ThinkPHP3.2.4 order方法注入
漏洞详情: 漏洞文件:./ThinkPHP\Library\Think\Db\Driver.class.php 中的 parseOrder方法: 这也是继上次order方法注入之后的修复手段. 可以看 ...
- SpringMVC听课笔记(六:视图和试图解析器)
1.spring mvc解析视图 2. 视图和视图解析器 3. 视图 4.常用的视图类 5.视图解析器 1) 2) 3) 4)JSTL 需要注意的是,配置了mvc:view-controller,为 ...
- java线程缓存刷新的疑问
https://www.jianshu.com/p/3c06ffbf0d52 import java.util.concurrent.TimeUnit; public class VolatileFo ...
- 通过spring statemmachine 自定义构建属于自己的状态机(两种方式)
spring 的stateMachine 相对于当前的版本,还是比较新颖的,但是对于合适的业务场景,使用起来还是十分的方便的.但是对于官网提供的文档,讲解的是十分的精简,要想更深入的了解其内部架构,只 ...
- linux 文件目录解释笔记(未完待续...)
目录 应放置档案内容 /bin 系统有很多放置执行档的目录,但/bin比较特殊.因为/bin放置的是在单人维护模式下还能够被操作的指令. 在/bin底下的指令可以被root与一般帐号所使用,主要有:c ...
- codeblocks输出中文乱码解决办法
在使用codeblocks进行编程的时候我发现控制台输出会出现中文乱码,就像这样: 所以很快我就问了老师,解决步骤如下: 一:如果源码是用codeblock编写的,打开Setting->Edit ...