mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-12 19:00:43 -04:00
2.1 KiB
2.1 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 595608ff8bcd7a50bd490181 | Numeri di Hailstone | 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 - Se
nèpariallora il successivondella sequenza= n/2 - Se
nèdispariallora il successivondella sequenza= (3 * n) + 1
La congettura di Collatz (non dimostrata) è che i numeri di Hailstone per qualsiasi numero iniziale arrivano sempre a 1.
I numeri di Hailstone (detti così perché i valori sono di solito soggetti a più discese e salite come la grandine in una nuvola), sono anche noti come sequenza di Collatz.
--instructions--
- Create a routine to generate the hailstone sequence for a number
- La tua funzione dovrebbe restituire un array con il numero sotto
limitche ha la più lunga sequenza di collatz e la lunghezza della sequenza. (Ma non mostrare la sequenza effettiva!)
--hints--
hailstoneSequence dovrebbe essere una funzione.
assert(typeof hailstoneSequence === 'function');
hailstoneSequence(30) dovrebbe restituire un array.
assert(Array.isArray(hailstoneSequence(30)));
hailstoneSequence(30) dovrebbe restituire [27, 112].
assert.deepEqual(hailstoneSequence(30), [27, 112]);
hailstoneSequence(50000) dovrebbe restituire [35655, 324].
assert.deepEqual(hailstoneSequence(50000), [35655, 324]);
hailstoneSequence(100000) dovrebbe restituire [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];
}