From 186f76d4f7409a3568b17de9715d3ce67cf3f079 Mon Sep 17 00:00:00 2001 From: rzmk Date: Wed, 20 Sep 2023 17:27:28 -0400 Subject: [PATCH] feat(pre-algebra): add is_factor and is_multiple --- ladderz/src/lib.rs | 2 +- ladderz/src/pre_algebra/unit1.rs | 125 ++++++++++++++++++++++++++---- notebooks/pre-algebra/unit1.ipynb | 97 ++++++++++++++++++++++- 3 files changed, 205 insertions(+), 19 deletions(-) diff --git a/ladderz/src/lib.rs b/ladderz/src/lib.rs index 13c6080..f73ed88 100644 --- a/ladderz/src/lib.rs +++ b/ladderz/src/lib.rs @@ -1,4 +1,4 @@ //! Implementations of mathematical and technical concepts in Rust. -/// Various pre-algebra implementations including multiples (planned), factor pairs, etc. +/// Various pre-algebra implementations including factor pairs, factors, multiples, and more. pub mod pre_algebra; diff --git a/ladderz/src/pre_algebra/unit1.rs b/ladderz/src/pre_algebra/unit1.rs index 8930e00..39097c8 100644 --- a/ladderz/src/pre_algebra/unit1.rs +++ b/ladderz/src/pre_algebra/unit1.rs @@ -1,14 +1,14 @@ use std::collections::HashSet; -/// Finds all factor pairs for a given positive integer. -/// +/// Finds all factor pairs for a positive integer `n`. +/// /// # Challenge -/// -/// Write a program that finds all the factor pairs for a given number `n`. -/// +/// +/// Write a program that finds all the factor pairs for a number `n`. +/// /// # Description -/// -/// Generates a `HashSet` of factor pairs for a given positive integer `n`. +/// +/// Generates a `HashSet` of factor pairs for a positive integer `n`. /// /// This function calculates and returns a `HashSet` containing all unique factor pairs /// of the input positive integer `n`. A factor pair is a pair of positive integers @@ -57,15 +57,15 @@ pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> { factor_pairs } -/// Finds all factors of a given positive integer. -/// +/// Finds all factors of a positive integer `n`. +/// /// # 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. +/// +/// Write a program that finds all the factors of a 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`. +/// +/// Generates a `HashSet` of factors for a 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 @@ -100,7 +100,7 @@ pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> { pub fn get_factors(n: u32) -> HashSet { let mut factors: HashSet = HashSet::new(); - for num in 1..n+1 { + for num in 1..n + 1 { if n % num == 0 { factors.insert(num); } @@ -108,6 +108,81 @@ pub fn get_factors(n: u32) -> HashSet { factors } +/// Checks if a positive integer `x` is a factor of another positive integer `y`. +/// +/// # Challenge +/// +/// Write a program that determines whether one positive integer is a factor of another. +/// +/// # Description +/// +/// Checks if a positive integer `x` is a factor of another positive integer `y`. +/// +/// A factor of `y` is a positive integer `x` where `y` is evenly divisible by `x` (i.e., `y % x == 0`). +/// +/// # Arguments +/// +/// * `x` - The positive integer to determine whether it is a factor of `y` or not. +/// * `y` - The positive integer for which the factor check of `x` is performed. +/// +/// # Returns +/// +/// `true` if `x` is a factor of `y`, `false` otherwise. +/// +/// # Examples +/// +/// ```rust +/// use ladderz::pre_algebra::unit1::is_factor; +/// +/// fn main() { +/// assert!(is_factor(2, 16)); // 2 is a factor of 16 +/// assert!(!is_factor(3, 16)); // 3 is not a factor of 16 +/// } +/// ``` +/// +/// # Note +/// +/// This function determines if `x` is a factor of `y` by checking if `y` is evenly divisible by `x` +/// (i.e., `y % x == 0`). +pub fn is_factor(x: u32, y: u32) -> bool { + y % x == 0 +} + +/// Checks if a positive integer `x` is a multiple of another positive integer `y`. +/// +/// # Challenge +/// +/// Write a program that determines whether one positive integer is a multiple of another. +/// +/// # Description +/// +/// Checks if a positive integer `x` is a multiple of another positive integer `y`. +/// +/// A multiple of `y` is a positive integer `x` where `x` is evenly divisible by `y` (i.e., `x % y == 0`). +/// +/// # Arguments +/// +/// * `x` - The positive integer to determine whether it is a multiple of `y` or not. +/// * `y` - The positive integer for which the multiple check of `x` is performed. +/// +/// # Returns +/// +/// `true` if `x` is a multiple of `y`, `false` otherwise. +/// +/// # Examples +/// +/// ```rust +/// use ladderz::pre_algebra::unit1::is_multiple; +/// +/// fn main() { +/// assert!(is_multiple(16, 2)); // 16 is a multiple of 2 +/// assert!(!is_multiple(16, 3)); // 16 is not a multiple of 3 +/// } +/// ``` +pub fn is_multiple(x: u32, y: u32) -> bool { + x % y == 0 +} + #[cfg(test)] mod tests { use super::*; @@ -133,4 +208,26 @@ mod tests { let expected_2: HashSet = [1, 2, 4, 8, 16].into(); assert_eq!(result_2, expected_2); } + + #[test] + fn test_is_factor() { + let result: bool = true; + let expected: bool = is_factor(2, 10); + assert_eq!(result, expected); + + let result_2: bool = false; + let expected_2: bool = is_factor(3, 10); + assert_eq!(result_2, expected_2); + } + + #[test] + fn test_is_multiple() { + let result: bool = true; + let expected: bool = is_multiple(10, 2); + assert_eq!(result, expected); + + let result_2: bool = false; + let expected_2: bool = is_multiple(11, 2); + assert_eq!(result_2, expected_2); + } } diff --git a/notebooks/pre-algebra/unit1.ipynb b/notebooks/pre-algebra/unit1.ipynb index 40e8487..1fd5f29 100644 --- a/notebooks/pre-algebra/unit1.ipynb +++ b/notebooks/pre-algebra/unit1.ipynb @@ -24,7 +24,7 @@ "\n", "> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/understanding-factor-pairs).\n", "\n", - "**Write a program that finds all the factor pairs for a given number $n$.**\n", + "**Write a program that finds all the factor pairs for a number $n$.**\n", "\n", "- Do not repeat any pairs (e.g., consider `(2, 8)` and `(8, 2)` as the same).\n", "- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n", @@ -38,7 +38,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -72,7 +72,9 @@ "\n", "> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-of-a-number).\n", "\n", - "**Write a program that finds all the factors of a given number $n$.** Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n", + "**Write a program that finds all the factors of a number $n$.**\n", + "\n", + "- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n", "\n", "For example for $n = 16$, the output of `get_factors(16)` may be:\n", "\n", @@ -83,7 +85,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -101,6 +103,93 @@ "assert get_factors(16) == {1, 2, 4, 8, 16}\n", "assert get_factors(120) == {1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120}" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Finding factors and multiples\n", + "\n", + "> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-and-multiples)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Write a program that checks if a number $x$ is a factor of $y$.**\n", + "\n", + "- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n", + "\n", + "For example, for $x = 2$ and $y = 10$, the output of `is_factor(2, 10)` may be:\n", + "\n", + "```\n", + "True\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def is_factor(x: int, y: int) -> bool:\n", + " return y % x == 0\n", + "\n", + "assert is_factor(2, 10) == True\n", + "assert is_factor(3, 10) == False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Write a program that checks if a number $x$ is a multiple of $y$.**\n", + "\n", + "- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n", + "\n", + "For example, for $x = 20$ and $y = 3$, the output of `is_multiple(20, 3)` may be:\n", + "\n", + "```\n", + "True\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def is_multiple(x: int, y: int) -> bool:\n", + " return x % y == 0\n", + "\n", + "assert is_multiple(10, 2) == True\n", + "assert is_multiple(10, 3) == False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Write a program that finds all multiples of a given number $n$ up to a given limit $l$.**\n", + "\n", + "- Assume that $n$ and $l$ are positive integers greater than or equal to 1 ($n, l \\in \\mathbb{Z}^+$).\n", + "\n", + "For example for $n = 3$ and $l = 20$, the output of `get_multiples(3, 20)` may be:\n", + "\n", + "```\n", + "{3, 6, 9, 12, 15, 18}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {