feat: add get_factors to pre-algebra unit1.rs

This commit is contained in:
rzmk 2023-09-19 20:04:42 -04:00
parent 1f107bbabe
commit 31040d7807
No known key found for this signature in database
2 changed files with 76 additions and 3 deletions

View file

@ -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;

View file

@ -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<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.
pub fn get_factors(n: u32) -> HashSet<u32> {
let mut factors: HashSet<u32> = 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<u32> = get_factors(1);
let expected: HashSet<u32> = [1].into();
assert_eq!(result, expected);
let result_2: HashSet<u32> = get_factors(16);
let expected_2: HashSet<u32> = [1, 2, 4, 8, 16].into();
assert_eq!(result_2, expected_2);
}
}