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; ...
随机推荐
- Map类型数据导出Excel--poi
https://blog.csdn.net/KevinChen2019/article/details/101064790 <dependency> <groupId>org. ...
- .NET5 它来了!微软大一统时代来临!
今天双11,Microsoft released.NET 5(在他们的开发博客上同时发布).新版本的重点是改进.NET Core 3.1: 更小的单文件应用程序.对 Windows ARM64的支持以 ...
- Hash Map集合和Hash Set集合
HashMap集合的使用 1.1.每个集合对象的创建(new) 1.2.从集合中添加元素 1.3.从集合中取出某个元素 1.4.遍历集合 public class HashMapTest { publ ...
- react空标签之The React Fragment
如何使用React.Fragment创建不可见的HTML标签 在研究Ant Design Pro项目中,在登录模块中,有React.Fragment的实际应用 接下来先看一个小demo,将返回值包装在 ...
- 3.centos 7执行service iptables save报错问题
1.报错 [root@localhost ~]# service iptables save The service command supports only basic LSB actions ( ...
- Wormholes (spfa)
一种路是双向的,路的长度是正值:另一种路是单向的,路的长度是负值: 如果有负环输出YES:否则输出NO:不同的路可能有相同的起点和终点:必须用邻接表 While exploring his many ...
- CF 1405E Fixed Point Removal【线段树上二分】
CF 1405E Fixed Point Removal[线段树上二分] 题意: 给定长度为\(n\)的序列\(A\),每次操作可以把\(A_i = i\)(即值等于其下标)的数删掉,然后剩下的数组 ...
- 【洛谷 p3379】模板-最近公共祖先(图论--倍增算法求LCA)
题目:给定一棵有根多叉树,请求出指定两个点直接最近的公共祖先. 解法:倍增. 1 #include<cstdio> 2 #include<cstdlib> 3 #include ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- 【noi 2.2_1751】分解因数(递归)
题意:问一个给定正整数的分解因数的方式数.N=a1*a2*...*ak(a1<=a2<=...<=ak). 解法:一步步分解该数,总方式数为一个个因数被分解的方案数之和. 可用大括号 ...