479. Largest Palindrome Product

Find the largest palindrome made from the product of two n-digit numbers.

Since the result could be very large, you should return the largest palindrome mod 1337.

 

Example:

Input: 2

Output: 987

Explanation: 99 x 91 = 9009, 9009 % 1337 = 987

 

Note:

The range of n is [1,8].

Rust Solution

struct Solution;

impl Solution {
    fn largest_palindrome(n: i32) -> i32 {
        if n == 1 {
            return 9;
        }
        let max = 10u64.pow(n as u32) - 1;
        for i in (0..max).rev() {
            let left: String = i.to_string();
            let right: String = i.to_string().chars().rev().collect();
            let palindrome = format!("{}{}", left, right);
            if let Ok(value) = palindrome.parse::<u64>() {
                let mut j = max;
                while j * j >= value {
                    if value % j == 0 {
                        return (value % 1337) as i32;
                    }
                    j -= 1;
                }
            }
        }
        0
    }
}

#[test]
fn test() {
    let n = 2;
    let res = 987;
    assert_eq!(Solution::largest_palindrome(n), res);
    let n = 1;
    let res = 9;
    assert_eq!(Solution::largest_palindrome(n), res);
    let n = 5;
    let res = 677;
    assert_eq!(Solution::largest_palindrome(n), res);
}

Having problems with this solution? Click here to submit an issue on github.