mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-06 19:00:52 -04:00
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5900f3891000cf542c50fe9c | Завдання 29: Різні степені | 1 | 301941 | problem-29-distinct-powers |
--description--
Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
22=4, 23=8, 24=16, 25=32
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
32=9, 33=27, 34=81, 35=243
42=16, 43=64, 44=256, 45=1024
52=25, 53=125, 54=625, 55=3125
Якщо їх потім розташувати в порядку зростання, виключивши повтори, ми отримаємо таку послідовність з 15 різних членів:
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
How many distinct terms are in the sequence generated by a^b for 2 ≤ a ≤ n and 2 ≤ b ≤ n?
--hints--
distinctPowers(15) має повернути число.
assert(typeof distinctPowers(15) === 'number');
distinctPowers(15) має повернути число 177.
assert.strictEqual(distinctPowers(15), 177);
distinctPowers(20) має повернути число 324.
assert.strictEqual(distinctPowers(20), 324);
distinctPowers(25) має повернути число 519.
assert.strictEqual(distinctPowers(25), 519);
distinctPowers(30) має повернути число 755.
assert.strictEqual(distinctPowers(30), 755);
--seed--
--seed-contents--
function distinctPowers(n) {
return n;
}
distinctPowers(30);
--solutions--
const distinctPowers = (n) => {
let list = [];
for (let a=2; a<=n; a++) {
for (let b=2; b<=n; b++) {
let term = Math.pow(a, b);
if (list.indexOf(term)===-1) list.push(term);
}
}
return list.length;
};