[Intermediate Algorithm] - Sum All Odd Fibonacci Numbers
题目
给一个正整数num,返回小于或等于num的斐波纳契奇数之和。
斐波纳契数列中的前几个数字是 1、1、2、3、5 和 8,随后的每一个数字都是前两个数字之和。
例如,sumFibs(4)应该返回 5,因为斐波纳契数列中所有小于4的奇数是 1、1、3。
测试用例
sumFibs(1)应该返回一个数字。sumFibs(1000)应该返回1785。sumFibs(4000000)应该返回4613732。sumFibs(4)应该返回5。sumFibs(75024)应该返回60696。sumFibs(75025)应该返回135721。
分析思路
斐波那契数第一个和第二个为 1 是固定的,所以初始数组可以设置为: var fibsArray = [1, 1];
然后根据最后一个数等于前两数之和设置下一个数组元素,这样就组成了需要的斐波那契数组。对于奇数直接计算提出就行。
代码
1.function sumFibs(num) {
2. var fibsArray = [1, 1];
3. var retVal = 1;
4.
5. while (fibsArray[fibsArray.length - 1] <= num) {
6. if (fibsArray[fibsArray.length - 1] % 2) {
7. retVal += fibsArray[fibsArray.length - 1];
8. }
9. fibsArray.push(fibsArray[fibsArray.length - 2] + fibsArray[fibsArray.length - 1]);
10. }
11. fibsArray.pop(); /* 去除最后一个大于 num 的数 */
12.
13. return retVal;
14.}
15.
16.sumFibs(4);
另一个不用到数组的方法,该方法主要是针对该题的
1.function sumFibs(num) {
2. var fibo = [1, 1];
3. var oddSum = 2;
4.
5. while(true){
6. var item = fibo[0] + fibo[1];
7. if(num < item){
8. return oddSum;
9. }
10. if(item % 2){
11. oddSum += item;
12. }
13. fibo[0] = fibo[1];
14. fibo[1] = item;
15. }
16.}
17.
18.sumFibs(4);
[Intermediate Algorithm] - Sum All Odd Fibonacci Numbers的更多相关文章
- Sum All Odd Fibonacci Numbers
function sumFibs(num) { //return num; var arr = [1,1]; var add = 2; while(true){ var item = arr[0] + ...
- Sum All Odd Fibonacci Numbers-freecodecamp算法题目
Sum All Odd Fibonacci Numbers 1.要求 给一个正整数num,返回小于或等于num的斐波纳契奇数之和. 斐波纳契数列中的前几个数字是 1.1.2.3.5 和 8,随后的每一 ...
- [Intermediate Algorithm] - Sum All Primes
题目 求小于等于给定数值的质数之和. 只有 1 和它本身两个约数的数叫质数.例如,2 是质数,因为它只能被 1 和 2 整除.1 不是质数,因为它只能被自身整除. 给定的数不一定是质数. 提示 For ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- [CodeForces - 447E] E - DZY Loves Fibonacci Numbers
E DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
随机推荐
- Ubuntu的shell执行过程
登录shell(login shell)会执行.bash_profile,.bash_profile中会执行.profile,.profile中会执行.bashrc 非登录shell(non-logi ...
- Linux系统学习之 二:新手必须掌握的Linux命令2
2018-10-03 22:20:48 一.文件目录管理命令 1.touch 命令 用于创建空白文件或设置文件的时间,格式为“touch [选项] [文件]”. 参数: -a :仅修改“读取时间(at ...
- FZU Problem 2082 过路费
Problem 2082 过路费 Accept: 875 Submit: 2839Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...
- TortoiseGit生成PuttyKey与GitHub的SSH进行关联
1.打开Puttygen 要到进度条满格为止,知道出现如下界面: 把上面的Key复制. 最后点击[Save private key]保存. 2.登录GitHub进行如下操作: Settings-> ...
- CentOS 6.9使用sudo时出现:“...不在 sudoers 文件中,此事将被报告”的问题解决
在终端切换root账号登录 su 修改/etc/sudoers文件 visudo 找到:root ALL=(ALL) ALL,修改成自己的账号: 保存即可,按Exc,输入”:wq!“,回车.
- /sys/power/state
kernel/power/main.c中: /** * state - control system power state. * * show() returns what states are s ...
- Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft.Build.En
错误具体信息: Unable to read the project file 'client.csproj'. Could not load file or assembly 'Microsoft. ...
- ArcGIS Python实现Modis NDVI批量化月最大合成
最大合成法(MVC)能够在Envi中的Band Math中进行,式子是B1>B2,可是无法批量化.本文实如今ArcGIS中利用Python代码批量进行,例如以下: 用到的Modis NDVI数据 ...
- [Linux]RedHat Linux 忘记rootpassword该怎样又一次设置password
1. 开机在出现grub画面,按e键,例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU3VubnlZb29uYQ==/font/5a6 ...
- 圆角矩形“RoundRectShape”使用详解
圆角矩形 常用作一些组件的背景 构造函数: RoundRectShape(float[] outerRadii, RectF inset, float[] innerRadii) Specifies ...