* 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>
5.2 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
598eef80ba501f1268170e1e | 斐波那契n步数序列 | 5 | fibonacci-n-step-number-sequences |
--description--
编写一个函数来生成Fibonacci n步数序列和Lucas序列。第一个参数是n。第二个参数是要返回的元素数。第三个参数将指定是输出Fibonacci序列还是Lucas序列。如果参数为“f”,则返回Fibonacci序列,如果为“l”,则返回Lucas序列。序列必须作为数组返回。更多细节如下:
这些数字序列是普通斐波纳契数列的扩展,其中:
对于$ n = 2 $,我们有Fibonacci序列;初始值$ \[1,1] $和$ F_k ^ 2 = F\_ {k-1} ^ 2 + F\_ {k-2} ^ 2 $对于$ n = 3 $,我们有tribonacci序列;初始值$ \[1,1,2] $和$ F_k ^ 3 = F\_ {k-1} ^ 3 + F\_ {k-2} ^ 3 + F\_ {k-3} ^ 3 $ $ $ = 4 $我们有tetranacci序列;初始值$ \[1,1,2,4] $和$ F_k ^ 4 = F\_ {k-1} ^ 4 + F\_ {k-2} ^ 4 + F\_ {k-3} ^ 4 + F\_ {k -4} ^ 4 $ ...对于一般的$ n> 2 $,我们有斐波那契$ n $ -step序列 - $ F_k ^ n $; $(n-1)$'斐波那契$ n $ -step序列$ F_k ^ {n-1} $的前$ n $值的初始值;和$ k $'这个$ n $'序列的值是$ F_k ^ n = \\ sum\_ {i = 1} ^ {(n)} {F\_ {ki} ^ {(n)}} $对于$ n $的小值, 希腊数字前缀有时用于单独命名每个系列。
{| style =“text-align:left;” border =“4”cellpadding =“2”cellspacing =“2”
| + Fibonacci $ n $ -step序列
| - style =“background-color:rgb(255,204,255);”
! $ n $ !!系列名称!!值
| -
| 2 ||斐波那契|| 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 ......
| -
| 3 || tribonacci || 1 1 2 4 7 13 24 44 81 149 274 504 927 1705 3136 ......
| -
| 4 || tetranacci || 1 1 2 4 8 15 29 56 108 208 401 773 1490 2872 5536 ......
| -
| 5 || pentanacci || 1 1 2 4 8 16 31 61 120 236 464 912 1793 3525 6930 ......
| -
| 6 || hexanacci || 1 1 2 4 8 16 32 63 125 248 492 976 1936 3840 7617 ......
| -
| 7 || heptanacci || 1 1 2 4 8 16 32 64 127 253 504 1004 2000 3984 7936 ......
| -
| 8 || octonacci || 1 1 2 4 8 16 32 64 128 255 509 1016 2028 4048 8080 ...
| -
| 9 || nonanacci || 1 1 2 4 8 16 32 64 128 256 511 1021 2040 4076 8144 ......
| -
| 10 || decanacci || 1 1 2 4 8 16 32 64 128 256 512 1023 2045 4088 8172 ...
|}
可以在更改初始值的位置生成联合序列:
Lucas系列将两个前面的值相加,例如$ n = 2 $的斐波那契数列,但使用$ [2,1] $作为其初始值。
--hints--
fib_luc
是一个功能。
assert(typeof fib_luc === 'function');
fib_luc(2,10,"f")
应返回[1,1,2,3,5,8,13,21,34,55]
。
assert.deepEqual(fib_luc(2, 10, 'f'), ans[0]);
fib_luc(3,15,"f")
应返回[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136]
。
assert.deepEqual(fib_luc(3, 15, 'f'), ans[1]);
fib_luc(4,15,"f")
应返回[1,1,2,4,8,15,29,56,108,208,401,773,1490,2872,5536]
。
assert.deepEqual(fib_luc(4, 15, 'f'), ans[2]);
fib_luc(2,10,"l")
应返回[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
fib_luc(2,10,"l")
[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76]
。
assert.deepEqual(fib_luc(2, 10, 'l'), ans[3]);
fib_luc(3,15,"l")
应返回[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]
fib_luc(3,15,"l")
[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ]
。
assert.deepEqual(fib_luc(3, 15, 'l'), ans[4]);
fib_luc(4,15,"l")
应该返回[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]
fib_luc(4,15,"l")
[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ]
。
assert.deepEqual(fib_luc(4, 15, 'l'), ans[5]);
fib_luc(5,15,"l")
应该返回[ 2, 1, 3, 6, 12, 24, 46, 91, 179, 352, 692, 1360, 2674, 5257, 10335 ]
。
assert.deepEqual(fib_luc(5, 15, 'l'), ans[6]);
--seed--
--after-user-code--
const ans = [[1,1,2,3,5,8,13,21,34,55],
[1,1,2,4,7,13,24,44,81,149,274,504,927,1705,3136],
[1,1,2,4,8,15,29,56,108,208,401,773,1490,2872,5536],
[ 2, 1, 3, 4, 7, 11, 18, 29, 47, 76],
[ 2, 1, 3, 6, 10, 19, 35, 64, 118, 217, 399, 734, 1350, 2483, 4567 ],
[ 2, 1, 3, 6, 12, 22, 43, 83, 160, 308, 594, 1145, 2207, 4254, 8200 ],
[ 2, 1, 3, 6, 12, 24, 46, 91, 179, 352, 692, 1360, 2674, 5257, 10335 ]];
--seed-contents--
function fib_luc(n, len, w) {
}
--solutions--
function fib_luc(n, len, w) {
function nacci(a, n, len) {
while (a.length < len) {
let sum = 0;
for (let i = Math.max(0, a.length - n); i < a.length; i++)
sum += a[i];
a.push(sum);
}
return a;
}
if(w=="f"){
return nacci(nacci([1,1], n, n), n, len);
}else{
return nacci(nacci([2,1], n, n), n, len);
}
}