You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
759 B

11 months ago
fn test_10(n: u32) -> bool {
let s = n.to_string();
let sv = s.chars().collect::<Vec<char>>();
let svr = s.chars().rev().collect::<Vec<char>>();
for i in 0..s.len() {
if sv[i] != svr[i] {
return false;
}
}
return true;
}
fn test_2(n: u32) -> bool {
let mut sv = Vec::new();
let mut a = n;
while a > 0 {
sv.push(a % 2);
a /= 2;
}
let mut svr = sv.clone();
svr.reverse();
for i in 0..sv.len() {
if sv[i] != svr[i] { return false; }
}
return true;
}
fn main() {
let mut sum = 0;
for n in 1..1_000_000 {
if test_10(n) && test_2(n) {
println!("{}", n);
sum += n;
}
}
println!("{}", sum);
}