Created
October 6, 2016 15:20
-
-
Save qolop/b4bc627ea4354d3f2ece66e9cbd09212 to your computer and use it in GitHub Desktop.
Factorization and primes
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
// Author: Patrick McCann | |
// Date: October 5, 2016 | |
fn main() { | |
if std::env::args().len() > 1 { | |
let n = std::env::args().nth(1).unwrap().parse::<i32>().unwrap(); | |
println!("Factors of {}: {:?}", n, factorize(n)); | |
println!("Primes up to {}: {:?}", n, primes(n)); | |
} else { | |
println!("Oops! Add the number you want to factorize as an argument.") | |
} | |
} | |
fn factorize(n: i32) -> Vec<i32> { | |
(1..n + 1).into_iter().filter(|x| n % x == 0).collect() | |
} | |
fn primes(n: i32) -> Vec<i32> { | |
(1..n + 1).into_iter().filter(|x| is_prime(*x)).collect::<Vec<i32>>() | |
} | |
fn is_prime(n: i32) -> bool { | |
!(2..n).into_iter().any(|x| n % x == 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment