Skip to content

Instantly share code, notes, and snippets.

@sumproxy
Created October 12, 2015 13:13
Show Gist options
  • Save sumproxy/a9341f87fee422aa1319 to your computer and use it in GitHub Desktop.
Save sumproxy/a9341f87fee422aa1319 to your computer and use it in GitHub Desktop.
Use std::collections::HashMap;
struct RangeTuple<T> {
cache: HashMap<(T, T), bool>,
first: std::ops::Range<T>,
second: std::ops::Range<T>,
}
impl<T: Hash + Eq> RangeTuple<T> {
fn new(f: std::ops::Range<T>, s: std::ops::Range<T>) -> RangeTuple<T> {
RangeTuple { first: f, second: s, cache: HashMap<(T, T), bool> = HashMap::new() }
}
}
impl<T> Iterator for RangeTuple<T> {
type Item = (T, T);
fn next(&mut self) -> Option<(T, T)> {
Some((self.first.next(), self.second.next()))
}
}
fn is_palindrome(num: u64) -> bool {
let s = num.to_string();
s.chars().zip(s.chars().rev()).all(|(i1, i2)| i1 == i2 )
}
fn main() {
let mut largest = 0;
for i in 100..999 {
for j in 100..999 {
if is_palindrome(i * j) && ((i * j) > largest) {
largest = i * j;
println!("Largest: {}, {} x {}", largest, i, j);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment