PerfectPower (Perfect power v1.0.0)

A module for detecting perfect powers in mathematics.

A perfect power is a positive integer that can be expressed as an integer power of another positive integer. More formally, a perfect power is a number n that can be written as $n = a^b$ where a and b are integers greater than 1.

Examples

iex> PerfectPower.exponential_form?(4)   # 2²
true
iex> PerfectPower.exponential_form?(8)   # 2³
true
iex> PerfectPower.exponential_form?(16)  # 2⁴ or 4²
true
iex> PerfectPower.exponential_form?(2)   # prime
false
iex> PerfectPower.exponential_form?(7)   # prime
false

Mathematical Background

Perfect powers include:

  • Perfect squares: 4 ($2^2$), 9 ($3^2$), 16 ($4^2$), 25 ($5^2$), etc.
  • Perfect cubes: 8 ($2^3$), 27 ($3^3$), 64 ($4^3$), 125 ($5^3$), etc.
  • Higher powers: 16 ($2^4$), 32 ($2^5$), 64 ($2^6$), 81 ($3^4$), etc.

Numbers that are NOT perfect powers include:

  • Prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, etc.
  • Products of different primes: 6 ($2 \times 3$), 10 ($2 \times 5$), 14 ($2 \times 7$), etc.
  • Other composite numbers that aren't perfect powers

Performance

The algorithm uses mathematical optimization to efficiently determine if a number is a perfect power by checking potential bases up to the square root of the number and using logarithmic bounds to limit the search space.

Edge Cases

  • 0 and 1 are considered perfect powers by convention ($0 = 0^2$, $1 = 1^2$)
  • 2 and 3 are not perfect powers (they are prime numbers)

Summary

Functions

Determines if a number can be written as a perfect power.

Functions

exponential_form?(n)

@spec exponential_form?(non_neg_integer()) :: boolean()

Determines if a number can be written as a perfect power.

A perfect power is a number that can be expressed as $a^b$ where both a and b are integers greater than 1.

Parameters

  • n - A non-negative integer to check

Returns

  • true if n is a perfect power
  • false if n is not a perfect power

Examples

iex> PerfectPower.exponential_form?(0)   # 0¹
true
iex> PerfectPower.exponential_form?(1)   # 1¹
true
iex> PerfectPower.exponential_form?(2)   # prime
false
iex> PerfectPower.exponential_form?(3)   # prime
false
iex> PerfectPower.exponential_form?(4)   # 2²
true
iex> PerfectPower.exponential_form?(8)   # 2³
true
iex> PerfectPower.exponential_form?(16)  # 2⁴ or 4²
true
iex> PerfectPower.exponential_form?(27)  # 3³
true
iex> PerfectPower.exponential_form?(100) # 10²
true
iex> PerfectPower.exponential_form?(101) # prime
false

Algorithm

The function uses an optimized approach:

  1. Handle edge cases (0, 1, 2, 3) with explicit pattern matching
  2. For other numbers, check potential bases from 2 up to the square root
  3. For each base, calculate the corresponding exponent using logarithms
  4. Verify if the calculated exponent produces the original number

This approach is efficient because:

  • It limits the search space using mathematical bounds
  • It uses logarithmic calculations to find potential exponents
  • It avoids brute force checking of all possible combinations

Mathematical Properties

  • If $n = a^b$, then $b \leq \log_2(n)$
  • The base $a$ must be $\leq \sqrt{n}$ for $b \geq 2$
  • This allows us to bound our search space efficiently