--- id: 5900f3891000cf542c50fe9c challengeType: 5 title: 'Problem 29: Distinct powers' --- ## Description
Consider all integer combinations of ab 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 If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: 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 ab for 2 ≤ a ≤ n and 2 ≤ b ≤ n?
## Instructions
## Tests
```yml tests: - text: distinctPowers(15) should return 177. testString: assert.strictEqual(distinctPowers(15), 177, 'distinctPowers(15) should return 177.'); - text: distinctPowers(20) should return 324. testString: assert.strictEqual(distinctPowers(20), 324, 'distinctPowers(20) should return 324.'); - text: distinctPowers(25) should return 519. testString: assert.strictEqual(distinctPowers(25), 519, 'distinctPowers(25) should return 519.'); - text: distinctPowers(30) should return 755. testString: assert.strictEqual(distinctPowers(30), 755, 'distinctPowers(30) should return 755.'); ```
## Challenge Seed
```js function distinctPowers(n) { // Good luck! return n; } distinctPowers(30); ```
## Solution
```js 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; }; ```