This commit is contained in:
rzmk 2023-09-22 14:16:52 +00:00
parent 4eed7e25fa
commit c202923651
9 changed files with 62 additions and 287 deletions

View file

@ -43,166 +43,34 @@
<a href="#43" id="43">43</a>
<a href="#44" id="44">44</a>
<a href="#45" id="45">45</a>
<a href="#46" id="46">46</a>
<a href="#47" id="47">47</a>
<a href="#48" id="48">48</a>
<a href="#49" id="49">49</a>
<a href="#50" id="50">50</a>
<a href="#51" id="51">51</a>
<a href="#52" id="52">52</a>
<a href="#53" id="53">53</a>
<a href="#54" id="54">54</a>
<a href="#55" id="55">55</a>
<a href="#56" id="56">56</a>
<a href="#57" id="57">57</a>
<a href="#58" id="58">58</a>
<a href="#59" id="59">59</a>
<a href="#60" id="60">60</a>
<a href="#61" id="61">61</a>
<a href="#62" id="62">62</a>
<a href="#63" id="63">63</a>
<a href="#64" id="64">64</a>
<a href="#65" id="65">65</a>
<a href="#66" id="66">66</a>
<a href="#67" id="67">67</a>
<a href="#68" id="68">68</a>
<a href="#69" id="69">69</a>
<a href="#70" id="70">70</a>
<a href="#71" id="71">71</a>
<a href="#72" id="72">72</a>
<a href="#73" id="73">73</a>
<a href="#74" id="74">74</a>
<a href="#75" id="75">75</a>
<a href="#76" id="76">76</a>
<a href="#77" id="77">77</a>
<a href="#78" id="78">78</a>
<a href="#79" id="79">79</a>
<a href="#80" id="80">80</a>
<a href="#81" id="81">81</a>
<a href="#82" id="82">82</a>
<a href="#83" id="83">83</a>
<a href="#84" id="84">84</a>
<a href="#85" id="85">85</a>
<a href="#86" id="86">86</a>
<a href="#87" id="87">87</a>
<a href="#88" id="88">88</a>
<a href="#89" id="89">89</a>
<a href="#90" id="90">90</a>
<a href="#91" id="91">91</a>
<a href="#92" id="92">92</a>
<a href="#93" id="93">93</a>
<a href="#94" id="94">94</a>
<a href="#95" id="95">95</a>
<a href="#96" id="96">96</a>
<a href="#97" id="97">97</a>
<a href="#98" id="98">98</a>
<a href="#99" id="99">99</a>
<a href="#100" id="100">100</a>
<a href="#101" id="101">101</a>
<a href="#102" id="102">102</a>
<a href="#103" id="103">103</a>
<a href="#104" id="104">104</a>
<a href="#105" id="105">105</a>
<a href="#106" id="106">106</a>
<a href="#107" id="107">107</a>
<a href="#108" id="108">108</a>
<a href="#109" id="109">109</a>
<a href="#110" id="110">110</a>
<a href="#111" id="111">111</a>
<a href="#112" id="112">112</a>
</pre></div><pre class="rust"><code><span class="doccomment">//! # 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&#39;s an example of using the `ladderz` crate to get the factors and factor pairs of a positive integer in sorted order.
//! We&#39;ll assume you&#39;re using Bash as your terminal.
//!
//! First let&#39;s create a new Rust project and change into the project directory:
//!
//! ```bash
//! cargo new my_ladderz_project
//! cd my_ladderz_project
//! ```
//!
//! Then let&#39;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 = &quot;https://github.com/rzmk/ladderz&quot;, branch = &quot;main&quot; }
//! ```
//!
//! Now in `src/main.rs` let&#39;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&lt;String&gt; = env::args().collect();
//!
//! // Check if input was provided
//! match args.get(1) {
//! Some(_) =&gt; {
//! match args[1].parse::&lt;u32&gt;() {
//! // Handle input that can be parsed as a u32
//! Ok(x) =&gt; {
//! // Convert the HashSet of factors of input x to a sorted Vec
//! let mut factors: Vec&lt;u32&gt; = get_factors(x).into_iter().collect::&lt;Vec&lt;u32&gt;&gt;();
//! factors.sort();
//!
//! // Convert the HashSet of factor pairs of input x to a sorted Vec
//! let mut factor_pairs: Vec&lt;(u32, u32)&gt; =
//! get_factor_pairs(x).into_iter().collect::&lt;Vec&lt;(u32, u32)&gt;&gt;();
//! factor_pairs.sort();
//!
//! // Print the results
//! println!(&quot;List of factors of {:?}: {:?}&quot;, x, factors);
//! println!(&quot;List of factor pairs of {:?}: {:?}&quot;, x, factor_pairs);
//! }
//! // Handle input that can&#39;t be parsed as a u32
//! Err(e) =&gt; println!(&quot;Error parsing input: {e}&quot;),
//! }
//! }
//! None =&gt; println!(&quot;No input provided.&quot;),
//! }
//! }
//!use ladderz::pre_algebra::get_factors;
//!
//!let x: u32 = 10;
//!println!(&quot;The factors of {x} are {:?}.&quot;, get_factors(x));
//! ```
//!
//! Now let&#39;s build the project&#39;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&#39;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&#39;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!(&quot;The factors of {x} are {:?}.&quot;, get_factors(x));
/// }
/// let x: u32 = 10;
/// println!(&quot;The factors of {x} are {:?}.&quot;, get_factors(x));
/// ```
///
/// ```console

View file

@ -200,17 +200,6 @@
<a href="#200" id="200">200</a>
<a href="#201" id="201">201</a>
<a href="#202" id="202">202</a>
<a href="#203" id="203">203</a>
<a href="#204" id="204">204</a>
<a href="#205" id="205">205</a>
<a href="#206" id="206">206</a>
<a href="#207" id="207">207</a>
<a href="#208" id="208">208</a>
<a href="#209" id="209">209</a>
<a href="#210" id="210">210</a>
<a href="#211" id="211">211</a>
<a href="#212" id="212">212</a>
<a href="#213" id="213">213</a>
</pre></div><pre class="rust"><code><span class="kw">use </span>std::collections::HashSet;
<span class="doccomment">/// 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&lt;(u32, u32)&gt; = [(1, 12), (2, 6), (3, 4)].into();
/// assert_eq!(result_pairs, expected_pairs);
/// }
/// let result_pairs = get_factor_pairs(12);
/// let expected_pairs: HashSet&lt;(u32, u32)&gt; = [(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&lt;u32&gt; = [1, 2, 4, 8, 16].into();
/// assert_eq!(result_factors, expected_factors);
/// }
/// let result_factors = get_factors(16);
/// let expected_factors: HashSet&lt;u32&gt; = [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
/// ```
</span><span class="kw">pub fn </span>is_multiple(x: u32, y: u32) -&gt; bool {
x % y == <span class="number">0
@ -344,23 +325,15 @@
/// use ladderz::pre_algebra::get_multiples_in_range;
/// use std::collections::HashSet;
///
/// fn main() {
/// let result: HashSet&lt;u32&gt; = get_multiples_in_range(2, 10);
/// let expected: HashSet&lt;u32&gt; = [2, 4, 6, 8, 10].into();
/// assert_eq!(result, expected);
///
/// let result: HashSet&lt;u32&gt; = get_multiples_in_range(3, 15);
/// let expected: HashSet&lt;u32&gt; = [3, 6, 9, 12, 15].into();
/// assert_eq!(result, expected);
/// }
/// let result: HashSet&lt;u32&gt; = get_multiples_in_range(2, 10);
/// let expected: HashSet&lt;u32&gt; = [2, 4, 6, 8, 10].into();
/// assert_eq!(result, expected);
/// ```
</span><span class="kw">pub fn </span>get_multiples_in_range(n: u32, end: u32) -&gt; HashSet&lt;u32&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>multiples: HashSet&lt;u32&gt; = HashSet::new();
<span class="kw">for </span>num <span class="kw">in </span>n..end + <span class="number">1 </span>{
<span class="kw">if </span>num % n == <span class="number">0 </span>{
multiples.insert(num);
}
<span class="kw">for </span>num <span class="kw">in </span>(n..end + <span class="number">1</span>).step_by(n <span class="kw">as </span>usize) {
multiples.insert(num);
}
multiples
}
@ -422,6 +395,11 @@
<span class="kw">let </span>result_2: HashSet&lt;u32&gt; = get_multiples_in_range(<span class="number">5</span>, <span class="number">34</span>);
<span class="kw">let </span>expected_2: HashSet&lt;u32&gt; = [<span class="number">5</span>, <span class="number">10</span>, <span class="number">15</span>, <span class="number">20</span>, <span class="number">25</span>, <span class="number">30</span>].into();
<span class="macro">assert_eq!</span>(result_2, expected_2);
<span class="comment">// Test when the range has no multiples
</span><span class="kw">let </span>result_3: HashSet&lt;u32&gt; = get_multiples_in_range(<span class="number">7</span>, <span class="number">11</span>);
<span class="kw">let </span>expected_3: HashSet&lt;u32&gt; = [<span class="number">7</span>].into();
<span class="macro">assert_eq!</span>(expected_3, result_3);
}
}
</code></pre></div></section></main></body></html>