Problem:
Score : 200 points

Problem Statement

You are given a positive integer X.
Find the largest perfect power that is at most X.
Here, a perfect power is an integer that can be represented as b^p, where b is an integer not less than 1 and p is an integer not less than 2.

Constraints

1 ≤ X ≤ 1000

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the largest perfect power that is at most X.

Sample Input 1

10

Sample Output 1

9

There are four perfect powers that are at most 10: 1, 4, 8 and 9.
We should print the largest among them, 9.

Sample Input 2

1

Sample Output 2

1

Sample Input 3

999

Sample Output 3

961

Solution in Ruby (2.3.3) (judge verdict: Wrong Answer)

def eratosthenes(limit)
  # 探索リスト
  search_list = []
  3.upto limit do |i|
    search_list << i if i.odd?
  end
  # p search_list

  # 探索結果（素数配列）
  primes = [2]

  # 最後に見つかった素数がコレを越えたら探索終了（残りは全て素数）
  root_limit = Math.sqrt(limit)

  while (prime = search_list.shift) do
    # 探索リストの先頭は必ず素数
    primes << prime

    # primeが√limit を越えたら、残りは全て素数
    if root_limit < prime
      primes.concat(search_list)
      break
    end

    # primeの倍数を全て除去
    search_list.reject! { |i| i % prime == 0 }
  end

  primes
end

def main
  # b**p <= 1000
  # の場合、最大の b は31

  # 素数配列
  primes = eratosthenes(31)

  lim = ARGF.gets.to_i
  # puts("Error") if n <= 0

  max_num = 1
  primes.each do |n|

    2.upto(10) do |p|
      cur = n**p
      break if lim < cur

      if max_num < cur
        max_num = cur
      end
    end
  end

  puts max_num
end

if __FILE__ == $0
  main
end