mirror of
https://github.com/rzmk/ladderz.git
synced 2025-12-19 06:59:25 +00:00
feat: use add method for get_multiples_in_range, simplify docs + no fn main
This commit is contained in:
parent
03c12af93f
commit
6bbf891052
3 changed files with 35 additions and 114 deletions
|
|
@ -2,95 +2,30 @@
|
||||||
//!
|
//!
|
||||||
//! Implementations of mathematical and technical concepts in Rust.
|
//! Implementations of mathematical and technical concepts in Rust.
|
||||||
//!
|
//!
|
||||||
//! # Subjects
|
//! # Installing the crate
|
||||||
//!
|
//!
|
||||||
//! The modules for currently supported subjects are:
|
//! To add the crate to your project, add the following dependency under your `[dependencies]` section in your `Cargo.toml`:
|
||||||
//!
|
|
||||||
//! - [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:
|
|
||||||
//!
|
//!
|
||||||
//! ```toml
|
//! ```toml
|
||||||
//! ladderz = { git = "https://github.com/rzmk/ladderz", branch = "main" }
|
//! 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
|
//! ```rust
|
||||||
//! use ladderz::pre_algebra::{get_factors, get_factor_pairs};
|
//!use ladderz::pre_algebra::get_factors;
|
||||||
//! 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));
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! 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
|
//! ```console
|
||||||
//! List of factors of 12: [1, 2, 3, 4, 6, 12]
|
//! The factors of 10 are {1, 5, 2, 10}.
|
||||||
//! 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](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.
|
/// Various pre-algebra implementations including factor pairs, factors, multiples, and more.
|
||||||
///
|
///
|
||||||
|
|
@ -99,10 +34,8 @@
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use ladderz::pre_algebra::get_factors;
|
/// use ladderz::pre_algebra::get_factors;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// let x: u32 = 10;
|
||||||
/// let x: u32 = 10;
|
/// println!("The factors of {x} are {:?}.", get_factors(x));
|
||||||
/// println!("The factors of {x} are {:?}.", get_factors(x));
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// ```console
|
/// ```console
|
||||||
|
|
|
||||||
|
|
@ -14,11 +14,9 @@ use std::collections::HashSet;
|
||||||
/// use std::collections::HashSet;
|
/// use std::collections::HashSet;
|
||||||
/// use ladderz::pre_algebra::get_factor_pairs;
|
/// use ladderz::pre_algebra::get_factor_pairs;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// let result_pairs = get_factor_pairs(12);
|
||||||
/// let result_pairs = get_factor_pairs(12);
|
/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
|
||||||
/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into();
|
/// assert_eq!(result_pairs, expected_pairs);
|
||||||
/// assert_eq!(result_pairs, expected_pairs);
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Note
|
/// # Note
|
||||||
|
|
@ -57,11 +55,9 @@ pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> {
|
||||||
/// use std::collections::HashSet;
|
/// use std::collections::HashSet;
|
||||||
/// use ladderz::pre_algebra::get_factors;
|
/// use ladderz::pre_algebra::get_factors;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// let result_factors = get_factors(16);
|
||||||
/// let result_factors = get_factors(16);
|
/// let expected_factors: HashSet<u32> = [1, 2, 4, 8, 16].into();
|
||||||
/// let expected_factors: HashSet<u32> = [1, 2, 4, 8, 16].into();
|
/// assert_eq!(result_factors, expected_factors);
|
||||||
/// assert_eq!(result_factors, expected_factors);
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Note
|
/// # Note
|
||||||
|
|
@ -89,10 +85,8 @@ pub fn get_factors(n: u32) -> HashSet<u32> {
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use ladderz::pre_algebra::is_factor;
|
/// use ladderz::pre_algebra::is_factor;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// assert!(is_factor(2, 16)); // 2 is 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
|
||||||
/// assert!(!is_factor(3, 16)); // 3 is not a factor of 16
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// # Note
|
/// # Note
|
||||||
|
|
@ -112,10 +106,8 @@ pub fn is_factor(x: u32, y: u32) -> bool {
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use ladderz::pre_algebra::is_multiple;
|
/// use ladderz::pre_algebra::is_multiple;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// assert!(is_multiple(16, 2)); // 16 is a multiple of 2
|
||||||
/// 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, 3)); // 16 is not a multiple of 3
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn is_multiple(x: u32, y: u32) -> bool {
|
pub fn is_multiple(x: u32, y: u32) -> bool {
|
||||||
x % y == 0
|
x % y == 0
|
||||||
|
|
@ -131,23 +123,15 @@ pub fn is_multiple(x: u32, y: u32) -> bool {
|
||||||
/// use ladderz::pre_algebra::get_multiples_in_range;
|
/// use ladderz::pre_algebra::get_multiples_in_range;
|
||||||
/// use std::collections::HashSet;
|
/// use std::collections::HashSet;
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// let result: HashSet<u32> = get_multiples_in_range(2, 10);
|
||||||
/// let result: HashSet<u32> = get_multiples_in_range(2, 10);
|
/// let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
|
||||||
/// let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
|
/// assert_eq!(result, expected);
|
||||||
/// 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);
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32> {
|
pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32> {
|
||||||
let mut multiples: HashSet<u32> = HashSet::new();
|
let mut multiples: HashSet<u32> = HashSet::new();
|
||||||
|
|
||||||
for num in n..end + 1 {
|
for num in (n..end + 1).step_by(n as usize) {
|
||||||
if num % n == 0 {
|
multiples.insert(num);
|
||||||
multiples.insert(num);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
multiples
|
multiples
|
||||||
}
|
}
|
||||||
|
|
@ -209,5 +193,10 @@ mod tests {
|
||||||
let result_2: HashSet<u32> = get_multiples_in_range(5, 34);
|
let result_2: HashSet<u32> = get_multiples_in_range(5, 34);
|
||||||
let expected_2: HashSet<u32> = [5, 10, 15, 20, 25, 30].into();
|
let expected_2: HashSet<u32> = [5, 10, 15, 20, 25, 30].into();
|
||||||
assert_eq!(result_2, expected_2);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -186,15 +186,14 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 5,
|
"execution_count": 1,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def get_multiples_in_range(n: int, end: int) -> set:\n",
|
"def get_multiples_in_range(n: int, end: int) -> set:\n",
|
||||||
" multiples = set()\n",
|
" multiples = set()\n",
|
||||||
" for num in range(n, end + 1):\n",
|
" for num in range(n, end + 1, n):\n",
|
||||||
" if num % n == 0:\n",
|
" multiples.add(num)\n",
|
||||||
" multiples.add(num)\n",
|
|
||||||
" return multiples\n",
|
" return multiples\n",
|
||||||
"\n",
|
"\n",
|
||||||
"assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",
|
"assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue