Expand description
ladderz
+Expand description
ladderz
Implementations of mathematical and technical concepts in Rust.
-Subjects
-The modules for currently supported subjects are:
--
-
- pre_algebra - Various pre-algebra implementations including factor pairs, factors, multiples, and more. -
Example
-Here’s an example of using the ladderz crate to get the factors and factor pairs of a positive integer in sorted order.
-We’ll assume you’re using Bash as your terminal.
First let’s create a new Rust project and change into the project directory:
-cargo new my_ladderz_project
-cd my_ladderz_project
-Then let’s add the following to Cargo.toml under the [dependencies] section:
Installing the crate
+To add the crate to your project, add the following dependency under your [dependencies] section in your Cargo.toml:
ladderz = { git = "https://github.com/rzmk/ladderz", branch = "main" }
-Now in src/main.rs let’s replace the contents with the following code:
Example
+use ladderz::pre_algebra::get_factors;
-use ladderz::pre_algebra::{get_factors, get_factor_pairs};
-use std::env;
-
-fn main() {
- // Get user input as a Vec
- let args: Vec<String> = env::args().collect();
-
- // Check if input was provided
- match args.get(1) {
- Some(_) => {
- match args[1].parse::<u32>() {
- // Handle input that can be parsed as a u32
- Ok(x) => {
- // Convert the HashSet of factors of input x to a sorted Vec
- let mut factors: Vec<u32> = get_factors(x).into_iter().collect::<Vec<u32>>();
- factors.sort();
-
- // Convert the HashSet of factor pairs of input x to a sorted Vec
- let mut factor_pairs: Vec<(u32, u32)> =
- get_factor_pairs(x).into_iter().collect::<Vec<(u32, u32)>>();
- factor_pairs.sort();
-
- // Print the results
- println!("List of factors of {:?}: {:?}", x, factors);
- println!("List of factor pairs of {:?}: {:?}", x, factor_pairs);
- }
- // Handle input that can't be parsed as a u32
- Err(e) => println!("Error parsing input: {e}"),
- }
- }
- None => println!("No input provided."),
- }
-}
+let x: u32 = 10;
+println!("The factors of {x} are {:?}.", get_factors(x));
+The factors of 10 are {1, 5, 2, 10}.
-Now let’s build the project’s binary file so we can run it from the command line:
-cargo build --release
-
-Our runnable binary file should be located at the local path ./target/release/my_ladders_project (or ./target/release/my_ladders_project.exe for Windows). Let’s run it with the positive integer 12 as input:
-./target/release/my_ladderz_project 12
-
-If you have a .exe file instead, you can run it with:
-./target/release/my_ladderz_project.exe 12
-
-The printed output should be:
-List of factors of 12: [1, 2, 3, 4, 6, 12]
-List of factor pairs of 12: [(1, 12), (2, 6), (3, 4)]
-
-Great! We’ve successfully used the ladderz crate to get the factors and factor pairs of a positive integer in sorted order.
+For a more detailed example of how to use the ladderz crate, please see the example on GitHub.
+Choose a module to view its available functions.
Modules
- Various pre-algebra implementations including factor pairs, factors, multiples, and more.
Function ladderz::pre_algebra::get_factor_pairs
source · pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)>Expand description
Finds all factor pairs for a positive integer n.
Function ladderz::pre_algebra::get_factor_pairs
source · pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)>Expand description
Finds all factor pairs for a positive integer n.
A factor pair is a pair of positive integers
(a, b) where a and b are both factors of n (i.e., a * b == n).
This function calculates and returns a HashSet<(u32, u32)> containing all unique factor pairs
@@ -7,11 +7,9 @@ of the input positive integer n.
use std::collections::HashSet;
use ladderz::pre_algebra::get_factor_pairs;
-fn main() {
- let result_pairs = get_factor_pairs(12);
- let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
- assert_eq!(result_pairs, expected_pairs);
-}Note
This function calculates factor pairs by iterating through positive integers from 1 to n
(inclusive) and checking if they divide n evenly. If they do, a factor pair (a, b) is
diff --git a/ladderz/pre_algebra/fn.get_factors.html b/ladderz/pre_algebra/fn.get_factors.html
index 8d26e99..04ca9d3 100644
--- a/ladderz/pre_algebra/fn.get_factors.html
+++ b/ladderz/pre_algebra/fn.get_factors.html
@@ -1,4 +1,4 @@
-
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.
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
@@ -7,11 +7,9 @@ of the input positive integer n.
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
diff --git a/ladderz/pre_algebra/fn.get_multiples_in_range.html b/ladderz/pre_algebra/fn.get_multiples_in_range.html
index 5b0c8b4..b921937 100644
--- a/ladderz/pre_algebra/fn.get_multiples_in_range.html
+++ b/ladderz/pre_algebra/fn.get_multiples_in_range.html
@@ -1,16 +1,10 @@
-
Function ladderz::pre_algebra::get_multiples_in_range
source · pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32>Expand description
Finds all the multiples of a positive integer n up to and including end (in the range [n, end]).
Function ladderz::pre_algebra::get_multiples_in_range
source · pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32>Expand description
Finds all the multiples of a positive integer n up to and including end (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).
Examples
use ladderz::pre_algebra::get_multiples_in_range;
use std::collections::HashSet;
-fn main() {
- let result: HashSet<u32> = get_multiples_in_range(2, 10);
- let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
- assert_eq!(result, expected);
-
- let result: HashSet<u32> = get_multiples_in_range(3, 15);
- let expected: HashSet<u32> = [3, 6, 9, 12, 15].into();
- assert_eq!(result, expected);
-}Function ladderz::pre_algebra::is_factor
source · pub fn is_factor(x: u32, y: u32) -> boolExpand description
Checks if a positive integer x is a factor of another positive integer y.
Function ladderz::pre_algebra::is_factor
source · pub fn is_factor(x: u32, y: u32) -> boolExpand 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).
Examples
use ladderz::pre_algebra::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).
Function ladderz::pre_algebra::is_multiple
source · pub fn is_multiple(x: u32, y: u32) -> boolExpand description
Checks if a positive integer x is a multiple of another positive integer y.
Function ladderz::pre_algebra::is_multiple
source · pub fn is_multiple(x: u32, y: u32) -> boolExpand 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).
Examples
use ladderz::pre_algebra::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
-}Example
use ladderz::pre_algebra::get_factors;
-fn main() {
- let x: u32 = 10;
- println!("The factors of {x} are {:?}.", get_factors(x));
-}The factors of 10 are {1, 5, 2, 10}.
Functions
- Finds all factor pairs for a positive integer
n. - Finds all factors of a positive integer
n. - Finds all the multiples of a positive integer
nup to and includingend(in the range [n, end]). - Checks if a positive integer
xis a factor of another positive integery. - Checks if a positive integer
xis a multiple of another positive integery.
//! # ladderz
//!
//! Implementations of mathematical and technical concepts in Rust.
//!
-//! # Subjects
+//! # Installing the crate
//!
-//! The modules for currently supported subjects are:
-//!
-//! - [pre_algebra] - Various pre-algebra implementations including factor pairs, factors, multiples, and more.
-//!
-//! # Example
-//!
-//! Here's an example of using the `ladderz` crate to get the factors and factor pairs of a positive integer in sorted order.
-//! We'll assume you're using Bash as your terminal.
-//!
-//! First let's create a new Rust project and change into the project directory:
-//!
-//! ```bash
-//! cargo new my_ladderz_project
-//! cd my_ladderz_project
-//! ```
-//!
-//! Then let's add the following to `Cargo.toml` under the `[dependencies]` section:
+//! To add the crate to your project, add the following dependency under your `[dependencies]` section in your `Cargo.toml`:
//!
//! ```toml
//! ladderz = { git = "https://github.com/rzmk/ladderz", branch = "main" }
//! ```
//!
-//! Now in `src/main.rs` let's replace the contents with the following code:
+//! # Example
//!
//! ```rust
-//! use ladderz::pre_algebra::{get_factors, get_factor_pairs};
-//! use std::env;
-//!
-//! fn main() {
-//! // Get user input as a Vec
-//! let args: Vec<String> = env::args().collect();
-//!
-//! // Check if input was provided
-//! match args.get(1) {
-//! Some(_) => {
-//! match args[1].parse::<u32>() {
-//! // Handle input that can be parsed as a u32
-//! Ok(x) => {
-//! // Convert the HashSet of factors of input x to a sorted Vec
-//! let mut factors: Vec<u32> = get_factors(x).into_iter().collect::<Vec<u32>>();
-//! factors.sort();
-//!
-//! // Convert the HashSet of factor pairs of input x to a sorted Vec
-//! let mut factor_pairs: Vec<(u32, u32)> =
-//! get_factor_pairs(x).into_iter().collect::<Vec<(u32, u32)>>();
-//! factor_pairs.sort();
-//!
-//! // Print the results
-//! println!("List of factors of {:?}: {:?}", x, factors);
-//! println!("List of factor pairs of {:?}: {:?}", x, factor_pairs);
-//! }
-//! // Handle input that can't be parsed as a u32
-//! Err(e) => println!("Error parsing input: {e}"),
-//! }
-//! }
-//! None => println!("No input provided."),
-//! }
-//! }
+//!use ladderz::pre_algebra::get_factors;
//!
+//!let x: u32 = 10;
+//!println!("The factors of {x} are {:?}.", get_factors(x));
//! ```
//!
-//! Now let's build the project's binary file so we can run it from the command line:
-//!
-//! ```bash
-//! cargo build --release
-//! ```
-//!
-//! Our runnable binary file should be located at the local path `./target/release/my_ladders_project` (or `./target/release/my_ladders_project.exe` for Windows). Let's run it with the positive integer `12` as input:
-//!
-//! ```bash
-//! ./target/release/my_ladderz_project 12
-//! ```
-//!
-//! If you have a `.exe` file instead, you can run it with:
-//!
-//! ```bash
-//! ./target/release/my_ladderz_project.exe 12
-//! ```
-//!
-//! The printed output should be:
-//!
//! ```console
-//! List of factors of 12: [1, 2, 3, 4, 6, 12]
-//! List of factor pairs of 12: [(1, 12), (2, 6), (3, 4)]
+//! The factors of 10 are {1, 5, 2, 10}.
//! ```
//!
-//! Great! We've successfully used the `ladderz` crate to get the factors and factor pairs of a positive integer in sorted order.
+//! For a more detailed example of how to use the `ladderz` crate, please see the [example on GitHub](https://github.com/rzmk/ladderz#example).
+//!
+//! Choose a module to view its available functions.
/// Various pre-algebra implementations including factor pairs, factors, multiples, and more.
///
@@ -211,10 +79,8 @@
/// ```rust
/// use ladderz::pre_algebra::get_factors;
///
-/// fn main() {
-/// let x: u32 = 10;
-/// println!("The factors of {x} are {:?}.", get_factors(x));
-/// }
+/// let x: u32 = 10;
+/// println!("The factors of {x} are {:?}.", get_factors(x));
/// ```
///
/// ```console
diff --git a/src/ladderz/pre_algebra/unit1.rs.html b/src/ladderz/pre_algebra/unit1.rs.html
index c584596..832092f 100644
--- a/src/ladderz/pre_algebra/unit1.rs.html
+++ b/src/ladderz/pre_algebra/unit1.rs.html
@@ -200,17 +200,6 @@
200
201
202
-203
-204
-205
-206
-207
-208
-209
-210
-211
-212
-213
use std::collections::HashSet;
/// Finds all factor pairs for a positive integer `n`.
@@ -227,11 +216,9 @@
/// use std::collections::HashSet;
/// use ladderz::pre_algebra::get_factor_pairs;
///
-/// fn main() {
-/// let result_pairs = get_factor_pairs(12);
-/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
-/// assert_eq!(result_pairs, expected_pairs);
-/// }
+/// let result_pairs = get_factor_pairs(12);
+/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
+/// assert_eq!(result_pairs, expected_pairs);
/// ```
///
/// # Note
@@ -270,11 +257,9 @@
/// 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);
-/// }
+/// let result_factors = get_factors(16);
+/// let expected_factors: HashSet<u32> = [1, 2, 4, 8, 16].into();
+/// assert_eq!(result_factors, expected_factors);
/// ```
///
/// # Note
@@ -302,10 +287,8 @@
/// ```rust
/// use ladderz::pre_algebra::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
-/// }
+/// assert!(is_factor(2, 16)); // 2 is a factor of 16
+/// assert!(!is_factor(3, 16)); // 3 is not a factor of 16
/// ```
///
/// # Note
@@ -325,10 +308,8 @@
/// ```rust
/// use ladderz::pre_algebra::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
-/// }
+/// 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
@@ -344,23 +325,15 @@
/// use ladderz::pre_algebra::get_multiples_in_range;
/// use std::collections::HashSet;
///
-/// fn main() {
-/// let result: HashSet<u32> = get_multiples_in_range(2, 10);
-/// let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
-/// assert_eq!(result, expected);
-///
-/// let result: HashSet<u32> = get_multiples_in_range(3, 15);
-/// let expected: HashSet<u32> = [3, 6, 9, 12, 15].into();
-/// assert_eq!(result, expected);
-/// }
+/// let result: HashSet<u32> = get_multiples_in_range(2, 10);
+/// let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
+/// assert_eq!(result, expected);
/// ```
pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32> {
let mut multiples: HashSet<u32> = HashSet::new();
- for num in n..end + 1 {
- if num % n == 0 {
- multiples.insert(num);
- }
+ for num in (n..end + 1).step_by(n as usize) {
+ multiples.insert(num);
}
multiples
}
@@ -422,6 +395,11 @@
let result_2: HashSet<u32> = get_multiples_in_range(5, 34);
let expected_2: HashSet<u32> = [5, 10, 15, 20, 25, 30].into();
assert_eq!(result_2, expected_2);
+
+ // Test when the range has no multiples
+ let result_3: HashSet<u32> = get_multiples_in_range(7, 11);
+ let expected_3: HashSet<u32> = [7].into();
+ assert_eq!(expected_3, result_3);
}
}