DEV Community

Discussion on: Project Euler #4 - Largest Palindrome Product

Collapse
 
jay profile image
Jay

Love to work on the challenges, please keep this series running.
Here's my Rust Solution: Playground

fn main() {
    let max = max_palindrome();
    println!("Max palindrome is {}, product of {} * {}", max.1, (max.0).0, (max.0).1);
}

fn max_palindrome() -> ((i32, i32), i32) {
    let range = 100..1000;
    let mut ans = ((0, 0), 0);
    for a in range.clone() {
        for b in range.clone() {
            let p = a * b;
            if is_palindrome(p) && p > ans.1 {
                ans.1 = p;
                ans.0 = (a ,b);
            }
        }
    }
    ans
}

fn is_palindrome(num: i32) -> bool {
    let str_num = num.to_string();
    str_num
        .chars()
        .zip(str_num.chars().rev())
        .all(|(c1, c2)| c1 == c2)
}