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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::{fastout, input};
use std::collections::HashMap;

#[fastout]
fn main() {
    input!(x: usize);
    if x == 1 {
        println!("{}", 1);
        return;
    }
    let aaa = era::sieve(1001);

    for i in (0..x + 1).rev() {
        let m = trial_division(i);
        let mut is_ok = true;
        for v in m.iter() {
            if v.1 % 2 != 0 || (*v.1 == 1 && aaa.contains(v.0) && m.len() == 1) {
                is_ok = false;
                break;
            }
        }

        if is_ok {
            println!("{}", i);
            return;
        }
    }
}

mod era {
    #[cfg(test)]
    mod tests {

        use super::sieve;

        #[test]
        fn era() {
            assert_eq!(sieve(100000000).len(), 5761455);
        }
    }

    /// エラトステネスの篩
    /// 少し早い
    /// 引数までの素数を返す
    pub fn sieve(n: usize) -> Vec<usize> {
        let mut ps: Vec<usize> = vec![2];
        let mut xs: Vec<bool> = vec![true; n / 2];

        let mut x = 3;
        while x * x <= n {
            let mut y = (x - 3) / 2;
            if xs[y] {
                ps.push(x);
                y += x;
                for v in xs.iter_mut().skip(y).step_by(x) {
                    *v = false;
                }
            }
            x += 2;
        }
        for v in (x..n + 1).step_by(2).filter(|x| xs[(*x - 3) / 2] == true) {
            ps.push(v);
        }
        ps
    }
}

fn trial_division(mut n: usize) -> HashMap<usize, usize> {
    let mut primes = HashMap::new();
    let mut i = 2;

    //  n を root n 以下の整数で割り切れるまで割っていく
    while i * i <= n {
        while n % i == 0 {
            n /= i;
            if primes.contains_key(&i) {
                let x = primes.get_mut(&i).unwrap();
                *x += 1;
            } else {
                primes.insert(i, 1);
            }
        }
        i += 1;
    }

    // 最後にnが素数になっている場合はそれ自身も素因数に含めて終了
    if n > 1 {
        if primes.contains_key(&n) {
            let x = primes.get_mut(&n).unwrap();
            *x += 1;
        } else {
            primes.insert(n, 1);
        }
    }
    primes
}