Function ladderz::pre_algebra::get_factors
source · pub fn get_factors(n: u32) -> HashSet<u32>Expand description
Finds all factors of a positive integer n.
A factor of n is a positive integer a where
n is evenly divisible by a (i.e., n % a == 0).
This function calculates and returns a HashSet<u32> containing all unique factors
of the input positive integer n.
Examples
use std::collections::HashSet;
use ladderz::pre_algebra::get_factors;
fn main() {
let result_factors = get_factors(16);
let expected_factors: HashSet<u32> = [1, 2, 4, 8, 16].into();
assert_eq!(result_factors, expected_factors);
}Note
This function calculates factors by iterating through positive integers from 1 to n
(inclusive) and checking if they divide n evenly. If they do, the factor is added to
the HashSet. The function ensures that factors are unique, so duplicates are not added.