diff --git a/ladderz/src/pre_algebra/unit1.rs b/ladderz/src/pre_algebra/unit1.rs index 39097c8..ef8fc9f 100644 --- a/ladderz/src/pre_algebra/unit1.rs +++ b/ladderz/src/pre_algebra/unit1.rs @@ -183,6 +183,54 @@ pub fn is_multiple(x: u32, y: u32) -> bool { x % y == 0 } +/// Returns a HashSet containing all the multiples of a positive integer `n` in the range [n, end]. +/// +/// # Challenge +/// +/// Write a program that finds all the multiples of a positive integer `n` in a given range. +/// +/// # Description +/// +/// Returns a HashSet containing all the multiples of a positive integer `n` in the range [n, end]. +/// +/// A multiple of `n` is a positive integer `num` where `num` is evenly divisible by `n` (i.e., `num % n == 0`). +/// +/// # Arguments +/// +/// * `n` - The positive integer for which multiples are to be found. +/// * `end` - The upper limit of the range for finding multiples. +/// +/// # Returns +/// +/// A HashSet containing all the multiples of `n` in the range [n, end]. +/// +/// # Examples +/// +/// ```rust +/// use ladderz::pre_algebra::unit1::get_multiples_in_range; +/// use std::collections::HashSet; +/// +/// fn main() { +/// let result: HashSet = get_multiples_in_range(2, 10); +/// let expected: HashSet = [2, 4, 6, 8, 10].into(); +/// assert_eq!(result, expected); +/// +/// let result: HashSet = get_multiples_in_range(3, 15); +/// let expected: HashSet = [3, 6, 9, 12, 15].into(); +/// assert_eq!(result, expected); +/// } +/// ``` +pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet { + let mut multiples: HashSet = HashSet::new(); + + for num in n..end+1 { + if num % n == 0 { + multiples.insert(num); + } + } + multiples +} + #[cfg(test)] mod tests { use super::*; @@ -230,4 +278,15 @@ mod tests { let expected_2: bool = is_multiple(11, 2); assert_eq!(result_2, expected_2); } + + #[test] + fn test_get_multiples_in_range() { + let result: HashSet = get_multiples_in_range(2, 20); + let expected: HashSet = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].into(); + assert_eq!(result, expected); + + let result_2: HashSet = get_multiples_in_range(5, 34); + let expected_2: HashSet = [5, 10, 15, 20, 25, 30].into(); + assert_eq!(result_2, expected_2); + } } diff --git a/notebooks/pre-algebra/unit1.ipynb b/notebooks/pre-algebra/unit1.ipynb index 6460825..a20384c 100644 --- a/notebooks/pre-algebra/unit1.ipynb +++ b/notebooks/pre-algebra/unit1.ipynb @@ -173,11 +173,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Write a program that finds all multiples of a given number $n$ up to a given limit $l$.**\n", + "**Write a program that finds all multiples of a positive integer `n` in the range `[n, end]`.**\n", "\n", - "- Assume that $n$ and $l$ are positive integers greater than or equal to 1 ($n, l \\in \\mathbb{Z}^+$).\n", + "- Assume that `n` and `end` are positive integers greater than or equal to 1.\n", "\n", - "For example for $n = 3$ and $l = 20$, the output of `get_multiples(3, 20)` may be:\n", + "For example for `n = 3` and `end = 20`, the output of `get_multiples_in_range(3, 20)` may be:\n", "\n", "```\n", "{3, 6, 9, 12, 15, 18}\n", @@ -190,15 +190,15 @@ "metadata": {}, "outputs": [], "source": [ - "def get_multiples(n: int, l: int) -> set:\n", + "def get_multiples_in_range(n: int, end: int) -> set:\n", " multiples = set()\n", - " for num in range(n, l + 1):\n", + " for num in range(n, end + 1):\n", " if num % n == 0:\n", " multiples.add(num)\n", " return multiples\n", "\n", - "assert get_multiples(3, 20) == {3, 6, 9, 12, 15, 18}\n", - "assert get_multiples(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}" + "assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n", + "assert get_multiples_in_range(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}" ] } ],