-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.rs
36 lines (27 loc) · 1.05 KB
/
4.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Problem 4 - Largest palindrome product
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
*/
use rayon::prelude::*;
fn largest_pallindromic_number(lower_bound: usize, upper_bound: usize) -> usize {
let mut products: Vec<usize> = vec![];
for i in lower_bound..(upper_bound + 1) {
for j in lower_bound..(upper_bound + 1) {
products.push(i * j);
}
}
// Filter for pallindromic numbers
let mut pallindromic = products
.par_iter()
.filter(|x| x.to_string() == x.to_string().chars().rev().collect::<String>())
.collect::<Vec<&usize>>();
// Return the largest value
pallindromic.sort();
pallindromic.reverse();
return *pallindromic[0];
}
pub fn main() {
let number = largest_pallindromic_number(100, 999);
println!("The largest pallindromic number made from the product of two three-digit numbers is {number}");
}