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; ...
随机推荐
- 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
[原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...
- c 越界 数组越界
int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...
- 【C++小知识】#define、enum、const的含义与用法
一.#define 含义 define是宏定义,编译器不对其进行错误检查,在预编译阶段处理,没有作用域限制属于全局常量,在程序中编译器会对定义的常量名以数值进行替换,且每次替换都分配内存,此方法对于大 ...
- 【LinuxShell】cp 用法详解
Linux cp命令主要用于复制文件或目录. -a:此选项通常在复制目录时使用,它保留链接.文件属性,并复制目录下的所有内容.其作用等于dpR参数组合. -d:复制时保留链接.这里所说的链接相当于Wi ...
- 你应该了解的25个JS技巧
目录 1. 类型检查小工具 2. 检查是否为空 3. 获取列表最后一项 4. 带有范围的随机数生成器 5. 随机 ID 生成器 6. 创建一个范围内的数字 7. 格式化 JSON 字符串,string ...
- PIGS_POJ1149
PIGS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20253 Accepted: 9252 Description ...
- 【ElasticSearch】 使用AWS云ES服务来分析程序日志
最近公司系统升级,有些API的调用接口达到了每天10几万的请求量.目前公司里的日志,都是写文本文件中的.为了能够更好的分析这些日志数据,公司采用了AWS 的 ElasticSearch服务来分析日志. ...
- 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...
- Tomcat Servlet工作原理
前言 Tomcat的启动过程 Web应用初始化 创建Servlet实例 初始化Servlet 执行service方法 前言 Servlet实际上就是一个java类,只不过可以和浏览器进行一些数据的交换 ...
- cocos2d-x 调试问题
1.昨天一个新功能,在xcode模拟器上测试没问题.后来打包安卓后,一直有问题 就又添加日志功能 # define CCLOGFUNC(s) ...