This commit is contained in:
rzmk 2023-09-20 21:28:01 +00:00
parent 5c3f77b954
commit 51817d3873
12 changed files with 267 additions and 28 deletions

View file

@ -4,6 +4,6 @@
<a href="#4" id="4">4</a>
</pre></div><pre class="rust"><code><span class="doccomment">//! 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.
</span><span class="kw">pub mod </span>pre_algebra;
</code></pre></div></section></main></body></html>

View file

@ -134,17 +134,114 @@
<a href="#134" id="134">134</a>
<a href="#135" id="135">135</a>
<a href="#136" id="136">136</a>
<a href="#137" id="137">137</a>
<a href="#138" id="138">138</a>
<a href="#139" id="139">139</a>
<a href="#140" id="140">140</a>
<a href="#141" id="141">141</a>
<a href="#142" id="142">142</a>
<a href="#143" id="143">143</a>
<a href="#144" id="144">144</a>
<a href="#145" id="145">145</a>
<a href="#146" id="146">146</a>
<a href="#147" id="147">147</a>
<a href="#148" id="148">148</a>
<a href="#149" id="149">149</a>
<a href="#150" id="150">150</a>
<a href="#151" id="151">151</a>
<a href="#152" id="152">152</a>
<a href="#153" id="153">153</a>
<a href="#154" id="154">154</a>
<a href="#155" id="155">155</a>
<a href="#156" id="156">156</a>
<a href="#157" id="157">157</a>
<a href="#158" id="158">158</a>
<a href="#159" id="159">159</a>
<a href="#160" id="160">160</a>
<a href="#161" id="161">161</a>
<a href="#162" id="162">162</a>
<a href="#163" id="163">163</a>
<a href="#164" id="164">164</a>
<a href="#165" id="165">165</a>
<a href="#166" id="166">166</a>
<a href="#167" id="167">167</a>
<a href="#168" id="168">168</a>
<a href="#169" id="169">169</a>
<a href="#170" id="170">170</a>
<a href="#171" id="171">171</a>
<a href="#172" id="172">172</a>
<a href="#173" id="173">173</a>
<a href="#174" id="174">174</a>
<a href="#175" id="175">175</a>
<a href="#176" id="176">176</a>
<a href="#177" id="177">177</a>
<a href="#178" id="178">178</a>
<a href="#179" id="179">179</a>
<a href="#180" id="180">180</a>
<a href="#181" id="181">181</a>
<a href="#182" id="182">182</a>
<a href="#183" id="183">183</a>
<a href="#184" id="184">184</a>
<a href="#185" id="185">185</a>
<a href="#186" id="186">186</a>
<a href="#187" id="187">187</a>
<a href="#188" id="188">188</a>
<a href="#189" id="189">189</a>
<a href="#190" id="190">190</a>
<a href="#191" id="191">191</a>
<a href="#192" id="192">192</a>
<a href="#193" id="193">193</a>
<a href="#194" id="194">194</a>
<a href="#195" id="195">195</a>
<a href="#196" id="196">196</a>
<a href="#197" id="197">197</a>
<a href="#198" id="198">198</a>
<a href="#199" id="199">199</a>
<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>
<a href="#214" id="214">214</a>
<a href="#215" id="215">215</a>
<a href="#216" id="216">216</a>
<a href="#217" id="217">217</a>
<a href="#218" id="218">218</a>
<a href="#219" id="219">219</a>
<a href="#220" id="220">220</a>
<a href="#221" id="221">221</a>
<a href="#222" id="222">222</a>
<a href="#223" id="223">223</a>
<a href="#224" id="224">224</a>
<a href="#225" id="225">225</a>
<a href="#226" id="226">226</a>
<a href="#227" id="227">227</a>
<a href="#228" id="228">228</a>
<a href="#229" id="229">229</a>
<a href="#230" id="230">230</a>
<a href="#231" id="231">231</a>
<a href="#232" id="232">232</a>
<a href="#233" id="233">233</a>
</pre></div><pre class="rust"><code><span class="kw">use </span>std::collections::HashSet;
<span class="doccomment">/// Finds all factor pairs for a given positive integer.
///
<span class="doccomment">/// 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
@ -193,15 +290,15 @@
factor_pairs
}
<span class="doccomment">/// Finds all factors of a given positive integer.
///
<span class="doccomment">/// 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
@ -236,7 +333,7 @@
</span><span class="kw">pub fn </span>get_factors(n: u32) -&gt; HashSet&lt;u32&gt; {
<span class="kw">let </span><span class="kw-2">mut </span>factors: HashSet&lt;u32&gt; = HashSet::new();
<span class="kw">for </span>num <span class="kw">in </span><span class="number">1</span>..n+<span class="number">1 </span>{
<span class="kw">for </span>num <span class="kw">in </span><span class="number">1</span>..n + <span class="number">1 </span>{
<span class="kw">if </span>n % num == <span class="number">0 </span>{
factors.insert(num);
}
@ -244,6 +341,81 @@
factors
}
<span class="doccomment">/// 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`).
</span><span class="kw">pub fn </span>is_factor(x: u32, y: u32) -&gt; bool {
y % x == <span class="number">0
</span>}
<span class="doccomment">/// 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
/// }
/// ```
</span><span class="kw">pub fn </span>is_multiple(x: u32, y: u32) -&gt; bool {
x % y == <span class="number">0
</span>}
<span class="attr">#[cfg(test)]
</span><span class="kw">mod </span>tests {
<span class="kw">use super</span>::<span class="kw-2">*</span>;
@ -269,5 +441,27 @@
<span class="kw">let </span>expected_2: HashSet&lt;u32&gt; = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>, <span class="number">16</span>].into();
<span class="macro">assert_eq!</span>(result_2, expected_2);
}
<span class="attr">#[test]
</span><span class="kw">fn </span>test_is_factor() {
<span class="kw">let </span>result: bool = <span class="bool-val">true</span>;
<span class="kw">let </span>expected: bool = is_factor(<span class="number">2</span>, <span class="number">10</span>);
<span class="macro">assert_eq!</span>(result, expected);
<span class="kw">let </span>result_2: bool = <span class="bool-val">false</span>;
<span class="kw">let </span>expected_2: bool = is_factor(<span class="number">3</span>, <span class="number">10</span>);
<span class="macro">assert_eq!</span>(result_2, expected_2);
}
<span class="attr">#[test]
</span><span class="kw">fn </span>test_is_multiple() {
<span class="kw">let </span>result: bool = <span class="bool-val">true</span>;
<span class="kw">let </span>expected: bool = is_multiple(<span class="number">10</span>, <span class="number">2</span>);
<span class="macro">assert_eq!</span>(result, expected);
<span class="kw">let </span>result_2: bool = <span class="bool-val">false</span>;
<span class="kw">let </span>expected_2: bool = is_multiple(<span class="number">11</span>, <span class="number">2</span>);
<span class="macro">assert_eq!</span>(result_2, expected_2);
}
}
</code></pre></div></section></main></body></html>