Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2016 18:54
Show Gist options
  • Save anonymous/63cab9a5049c773cc51236c98eb84570 to your computer and use it in GitHub Desktop.
Save anonymous/63cab9a5049c773cc51236c98eb84570 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
// Project Euler Problem 36: Double-base palindromes
fn binary(n: i32) -> String {
format!("{:b}", n)
}
fn decimal(n: i32) -> String {
n.to_string()
}
fn is_palindrome(s: String) -> bool {
let s1 = s.into_bytes();
let mut s2 = s1.clone();
s2.reverse();
s1 == s2
}
fn main() {
let mut sum = 0;
for n in 1..1000000 {
if is_palindrome(binary(n)) && is_palindrome(decimal(n)) {
//println!("{:b}, {}", n, n);
sum += n
}
}
println!("{}", sum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment