Created
July 8, 2016 15:12
-
-
Save qolop/71ef78c394db822756d58cac9993db77 to your computer and use it in GitHub Desktop.
A one liner to find factors of a number 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
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