Rust Implementation of Problem 4

View source code here on GitHub!

Problem Solution

fn p0004::is_palindrome(x: u32) -> bool
pub fn problems::p0004::p0004() -> utils::Answer
 1/*
 2Project Euler Problem 4
 3
 4I couldn't figure out how to do this as efficiently as I would have liked. I am
 5SURE that there is a better way to check if a number is a palindrome, but I
 6could not think of one.
 7
 8Problem:
 9
10A palindromic number reads the same both ways. The largest palindrome made from
11the product of two 2-digit numbers is 9009 = 91 × 99.
12
13Find the largest palindrome made from the product of two 3-digit numbers.
14*/
15use itertools::Itertools;
16
17use crate::include::utils::{is_palindrome,Answer};
18
19pub fn p0004() -> Answer {
20    let mut answer: u32 = 0;
21    for v in (100..1000).combinations(2) {
22        let p: u32 = v.into_iter().product();
23        if is_palindrome(p) && p > answer {
24            answer = p;
25        }
26    }
27    return Answer::Int(answer.into());
28}

Tags: palindrome