freeCodeCamp/curriculum/challenges/japanese/10-coding-interview-prep/project-euler/problem-29-distinct-powers.md

1.9 KiB

id title challengeType forumTopicId dashedName
5900f3891000cf542c50fe9c 問題 29: 相異なる累乗 1 301941 problem-29-distinct-powers

--description--

2 ≤ a ≤ 5 かつ 2 ≤ b ≤ 5 に対し、整数の組み合わせ a^b をすべて考えます。

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

これらを昇順に並べ、重複する数を削除すると、次のような 15 項からなる数列になります。

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

2 ≤ an かつ 2 ≤ bn のとき、a^bで得られる数列の中に、相異なる項はいくつありますか。

--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;
};