Browse Source

036 ok

master
Coin de Gamma 11 months ago
parent
commit
0fe2743259
  1. 37
      036/main.rs

37
036/main.rs

@ -0,0 +1,37 @@
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);
}
Loading…
Cancel
Save