Last active
April 6, 2018 18:15
-
-
Save marioidival/5fa6b8b21f3bfb2136dea2eea3fd15c9 to your computer and use it in GitHub Desktop.
Solve challenges in Rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Found palindrome numeric | |
fn is_palindrome_numeric(n: String) -> bool { | |
n == n.chars().rev().collect::<String>() | |
} | |
fn main() { | |
for i in 1..20 { | |
if is_palindrome_numeric(format!("{}", i)) { | |
println!("{}", i) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Solve prime number with Sieve of Eratosthenes | |
fn sieve(list: &[i64]) { | |
if let Some((head, tail)) = list.split_first() { | |
let ys: Vec<i64> = tail.iter().filter(|n| *n % head != 0).cloned().collect(); | |
print!("{}, ", head); | |
sieve(&ys[..]); | |
} | |
} | |
fn main() { | |
let numbers: Vec<i64> = (2..10_000).map(|n| n).collect(); | |
sieve(&numbers[..]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment