Skip to content

Instantly share code, notes, and snippets.

@qolop
Created July 8, 2016 15:12
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qolop/71ef78c394db822756d58cac9993db77 to your computer and use it in GitHub Desktop.
Save qolop/71ef78c394db822756d58cac9993db77 to your computer and use it in GitHub Desktop.
A one liner to find factors of a number in Rust.
use std::io;
fn main() {
println!("Enter a number to find its factors: ");
let mut s = String::new();
io::stdin()
.read_line(&mut s)
.expect("failed to read line");
let num: u64 = s.trim().parse().expect("Enter in a positive integer only");
let factors = get_factors_functional(num);
println!("{:?}", factors);
}
fn get_factors_functional(n: u64) -> Vec<u64> {
(1..n + 1).into_iter().filter(|&x| n % x == 0).collect::<Vec<u64>>()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment