mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-13 13:00:15 -04:00
Co-authored-by: Naomi Carrigan <nhcarrigan@gmail.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 595608ff8bcd7a50bd490181 | ヘイルストーンシーケンス (コラッツ数列) | 1 | 302279 | hailstone-sequence |
--description--
The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
- If
nis1then the sequence ends nがeven(偶数) の場合、シーケンスの 次のnは= n/2nがodd(奇数) の場合、シーケンスの次のnは= (3 * n) + 1
コラッツの予想 (証明されていない) では、初期値が何であれ、ヘイルストーンシーケンスは常に終了するとされています。
ヘイルストーンシーケンスは、ヘイルストーン数としても知られています (値が通常は雲の中の雹 (ヘイルストーン) のように何度も上下するためです)。あるいはコラッツ数列とも呼ばれます。
--instructions--
- Create a routine to generate the hailstone sequence for a number
- この関数は、最長のヘイルストーンシーケンスを取る
limit未満の数とそのシーケンスの長さを持つ配列を返す必要があります。 (ただし、実際のシーケンスは表示しないでください!)
--hints--
hailstoneSequence は関数とします。
assert(typeof hailstoneSequence === 'function');
hailstoneSequence(30) は配列を返す必要があります。
assert(Array.isArray(hailstoneSequence(30)));
hailstoneSequence(30) は [27, 112] を返す必要があります。
assert.deepEqual(hailstoneSequence(30), [27, 112]);
hailstoneSequence(50000) は [35655, 324] を返す必要があります。
assert.deepEqual(hailstoneSequence(50000), [35655, 324]);
hailstoneSequence(100000) は [77031, 351] を返す必要があります。
assert.deepEqual(hailstoneSequence(100000), [77031, 351]);
--seed--
--seed-contents--
function hailstoneSequence(limit) {
const res = [];
return res;
}
--solutions--
function hailstoneSequence (limit) {
function hailstone(n) {
const seq = [n];
while (n > 1) {
n = n % 2 ? 3 * n + 1 : n / 2;
seq.push(n);
}
return seq;
}
let n = 0;
let max = 0;
for (let i = limit; --i;) {
const seq = hailstone(i);
const sLen = seq.length;
if (sLen > max) {
n = i;
max = sLen;
}
}
return [n, max];
}