* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
1.4 KiB
1.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
a5229172f011153519423690 | 求斐波那契数列中的奇数之和 | 5 | 16084 | sum-all-odd-fibonacci-numbers |
--description--
在这道题目中,我们需要写一个函数,参数为一个正整数 num
,返回值为斐波那契数列中,小于或等于 num
的奇数之和。
斐波那契数列中,第一和第二个数字都是 1,后面的每个数字由之前两数相加得出。斐波那契数列的前六个数字分别为:1、1、2、3、5、8。
比如,sumFibs(10)
应该返回 10
。因为斐波那契数列中,比 10
小的数字只有 1、1、3、5。
--hints--
sumFibs(1)
应返回一个数字。
assert(typeof sumFibs(1) === 'number');
sumFibs(1000)
应返回 1785。
assert(sumFibs(1000) === 1785);
sumFibs(4000000)
应返回 4613732。
assert(sumFibs(4000000) === 4613732);
sumFibs(4)
应返回 5。
assert(sumFibs(4) === 5);
sumFibs(75024)
应返回 60696。
assert(sumFibs(75024) === 60696);
sumFibs(75025)
应返回 135721。
assert(sumFibs(75025) === 135721);
--seed--
--seed-contents--
function sumFibs(num) {
return num;
}
sumFibs(4);
--solutions--
function sumFibs(num) {
var a = 1;
var b = 1;
var s = 0;
while (a <= num) {
if (a % 2 !== 0) {
s += a;
}
a = [b, b=b+a][0];
}
return s;
}