Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/project-euler/problem-29-distinct-powers.english.md
Kristofer Koishigawa 6cfd0fc503 fix: improve Project Euler descriptions, challenge seeds, and test cases (#38016)
* fix: improve Project Euler descriptions and test case

Improve formatting of Project Euler test descriptions. Also add poker hands array and new test case for problem 54

* feat: add typeof tests and gave functions proper names for first 100 challenges

* fix: continue fixing test descriptions and adding "before test" sections

* fix: address review comments

* fix: adjust grids in 18 and 67 and fix some text that reference files rather than the given arrays

* fix: implement bug fixes and improvements from review

* fix: remove console.log statements from seed and solution
2020-02-28 06:39:47 -06:00

2.2 KiB

id, challengeType, title, forumTopicId
id challengeType title forumTopicId
5900f3891000cf542c50fe9c 5 Problem 29: Distinct powers 301941

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 ≤ an and 2 ≤ bn?

Instructions

Tests

tests:
  - text: <code>distinctPowers(15)</code> should return a number.
    testString: assert(typeof distinctPowers(15) === 'number');
  - 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;
};