From 0fe27432593790ab8ac3266360b32db306e2d744 Mon Sep 17 00:00:00 2001 From: Coin de Gamma Date: Tue, 5 Dec 2023 08:28:00 +0000 Subject: [PATCH] 036 ok --- 036/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 036/main.rs diff --git a/036/main.rs b/036/main.rs new file mode 100644 index 0000000..15871ab --- /dev/null +++ b/036/main.rs @@ -0,0 +1,37 @@ +fn test_10(n: u32) -> bool { + let s = n.to_string(); + let sv = s.chars().collect::>(); + let svr = s.chars().rev().collect::>(); + 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); + +}