1.7 KiB
1.7 KiB
id | challengeType | title | forumTopicId |
---|---|---|---|
5900f3891000cf542c50fe9c | 5 | Problem 29: Distinct powers | 301941 |
Description
Instructions
Tests
tests:
- text: <code>distinctPowers(15)</code> should return 177.
testString: assert.strictEqual(distinctPowers(15), 177);
- text: <code>distinctPowers(20)</code> should return 324.
testString: assert.strictEqual(distinctPowers(20), 324);
- text: <code>distinctPowers(25)</code> should return 519.
testString: assert.strictEqual(distinctPowers(25), 519);
- text: <code>distinctPowers(30)</code> should return 755.
testString: assert.strictEqual(distinctPowers(30), 755);
Challenge Seed
function distinctPowers(n) {
// Good luck!
return n;
}
distinctPowers(30);
Solution
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;
};