diff --git a/ladderz/src/lib.rs b/ladderz/src/lib.rs index b3acc00..13c6080 100644 --- a/ladderz/src/lib.rs +++ b/ladderz/src/lib.rs @@ -1,4 +1,4 @@ -//! Non-production implementations of mathematical and technical concepts in Rust. +//! Implementations of mathematical and technical concepts in Rust. /// Various pre-algebra implementations including multiples (planned), factor pairs, etc. pub mod pre_algebra; diff --git a/ladderz/src/pre_algebra/unit1.rs b/ladderz/src/pre_algebra/unit1.rs index c15d677..8930e00 100644 --- a/ladderz/src/pre_algebra/unit1.rs +++ b/ladderz/src/pre_algebra/unit1.rs @@ -1,6 +1,13 @@ use std::collections::HashSet; -// TODO: Implement negative integers +/// Finds all factor pairs for a given positive integer. +/// +/// # Challenge +/// +/// Write a program that finds all the factor pairs for a given number `n`. +/// +/// # Description +/// /// Generates a `HashSet` of factor pairs for a given positive integer `n`. /// /// This function calculates and returns a `HashSet` containing all unique factor pairs @@ -50,14 +57,80 @@ pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> { factor_pairs } +/// Finds all factors of a given positive integer. +/// +/// # Challenge +/// +/// Write a program that finds all the factors of a given number. Assume that `n` is a positive integer greater than or equal to 1. +/// +/// # Description +/// +/// Generates a `HashSet` of factors for a given positive integer `n`. +/// +/// This function calculates and returns a `HashSet` containing all unique factors +/// of the input positive integer `n`. A factor of `n` is a positive integer `a` where +/// `n` is evenly divisible by `a` (i.e., `n % a == 0`). +/// +/// # Arguments +/// +/// * `n` - The positive integer for which factors are to be calculated. +/// +/// # Returns +/// +/// A `HashSet` containing all unique factors of the input integer `n`. +/// +/// # Examples +/// +/// ```rust +/// use std::collections::HashSet; +/// use ladderz::pre_algebra::unit1::get_factors; +/// +/// fn main() { +/// let result_factors = get_factors(16); +/// let expected_factors: HashSet = [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. +pub fn get_factors(n: u32) -> HashSet { + let mut factors: HashSet = HashSet::new(); + + for num in 1..n+1 { + if n % num == 0 { + factors.insert(num); + } + } + factors +} + #[cfg(test)] mod tests { use super::*; #[test] - fn test_get_factor_pairs_1() { + fn test_get_factor_pairs() { let result: HashSet<(u32, u32)> = get_factor_pairs(1); let expected: HashSet<(u32, u32)> = [(1, 1)].into(); assert_eq!(result, expected); + + let result_2: HashSet<(u32, u32)> = get_factor_pairs(16); + let expected_2: HashSet<(u32, u32)> = [(1, 16), (2, 8), (4, 4)].into(); + assert_eq!(result_2, expected_2); + } + + #[test] + fn test_get_factors() { + let result: HashSet = get_factors(1); + let expected: HashSet = [1].into(); + assert_eq!(result, expected); + + let result_2: HashSet = get_factors(16); + let expected_2: HashSet = [1, 2, 4, 8, 16].into(); + assert_eq!(result_2, expected_2); } }