Problem #29 Difficulty: 5%

Distinct Powers

Solution Language: Java

Problem Statement

Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

  • 2² = 4, 2³ = 8, 2⁴ = 16, 2⁵ = 32
  • 3² = 9, 3³ = 27, 3⁴ = 81, 3⁵ = 243
  • 4² = 16, 4³ = 64, 4⁴ = 256, 4⁵ = 1024
  • 5² = 25, 5³ = 125, 5⁴ = 625, 5⁵ = 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 a^b for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?

Approach

The solution involves:

  1. Generating all values of a^b for the given ranges
  2. Using a Set data structure to automatically eliminate duplicates
  3. Using BigInteger to handle large power values
  4. Counting the distinct terms in the resulting set