Skip to content

Instantly share code, notes, and snippets.

@qolop
Created October 6, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qolop/b4bc627ea4354d3f2ece66e9cbd09212 to your computer and use it in GitHub Desktop.
Save qolop/b4bc627ea4354d3f2ece66e9cbd09212 to your computer and use it in GitHub Desktop.
Factorization and primes
// 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