avatarapplied.math.coding

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1595

Abstract

code> or to use the result for <code>a = 0</code>.</p><h2 id="9e2b">Implementation:</h2><p id="64e3">The implementation is done in Rust. In case you need a little wrap-up, feel invited to this <a href="https://medium.com/@applied-math-coding/list/an-introduction-into-rust-22c99777c5e5">introduction</a>.</p><div id="7721"><pre><span class="hljs-comment">// Produces a = m * b + r for b > 0 and returns (m, r).</span> <span class="hljs-keyword">pub</span> <span class="hljs-keyword">fn</span> <span class="hljs-title function_">divide</span>(a: <span class="hljs-type">u64</span>, b: <span class="hljs-type">u64</span>) <span class="hljs-punctuation">-></span> <span class="hljs-type">Result</span><(<span class="hljs-type">u64</span>, <span class="hljs-type">u64</span>), <span class="hljs-type">String</span>> { <span class="hljs-keyword">if</span> b == <span class="hljs-number">0</span> { <span class="hljs-title function_ invoke__">Err</span>(<span class="hljs-type">String</span>::<span class="hljs-title function_ invoke__">from</span>(<span class="hljs-string">"Cannot divide by 0!"</span>)) } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> b > a { <span class="hljs-title function_ invoke__">Ok</span>((<span class="hljs-number">0</span>, a)) } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> a == <span class="hljs-number">1</span> { <span class="hljs-title function_ invoke__">Ok</span>((<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)) } <span cla

Options

ss="hljs-keyword">else</span> { <span class="hljs-keyword">match</span> <span class="hljs-title function_ invoke__">divide</span>(a - <span class="hljs-number">1</span>, b) { <span class="hljs-title function_ invoke__">Ok</span>((m, r)) => { <span class="hljs-keyword">if</span> r + <span class="hljs-number">1</span> == b { <span class="hljs-title function_ invoke__">Ok</span>((m + <span class="hljs-number">1</span>, <span class="hljs-number">0</span>)) } <span class="hljs-keyword">else</span> { <span class="hljs-title function_ invoke__">Ok</span>((m, r + <span class="hljs-number">1</span>)) } } <span class="hljs-title function_ invoke__">Err</span>(s) => <span class="hljs-title function_ invoke__">Err</span>(s), } } }</pre></div><p id="ed08">This code resembles one to one the proof above and it is not hard to adapt the code to also work for negative <code>a, b</code>, i.e. <code>i32, i64, ...</code>.</p><p id="a2a3">More interesting though is the way how to provide an implementation that supports <code>u32, u64, u128</code> at the same time. A technique how to accomplish this, can be found <a href="https://readmedium.com/how-to-overload-methods-in-rust-93bd5d71096a">here</a>.</p><p id="2f89">You can run the code in this <a href="https://play.rust-lang.org/?version=stable&amp;mode=release&amp;edition=2021&amp;gist=818eefd9b2811e28c02c8ae845a38bf0">playground</a>.</p><p id="ec96">Thank you for reading!</p></article></body>

The Euclidean Division Algorithm in Rust.

The Euclidean division is a well-known method to divide two integers by each other and having the result split into a unique integer part and a unique remainder. In this short story we are going to look at how to implement this algorithm in Rust. But first let us consider some theory behind this division.

The proof of this proposition, as typical for inductive conclusions, actually provides us with an algorithm which we will implement next.

So, for given a the idea is to reduce the problem to a-1 or to use the result for a = 0.

Implementation:

The implementation is done in Rust. In case you need a little wrap-up, feel invited to this introduction.

// Produces a = m * b + r for b > 0 and returns (m, r).
pub fn divide(a: u64, b: u64) -> Result<(u64, u64), String> {
    if b == 0 {
        Err(String::from("Cannot divide by 0!"))
    } else if b > a {
        Ok((0, a))
    } else if a == 1 {
        Ok((1, 0))
    } else {
        match divide(a - 1, b) {
            Ok((m, r)) => {
                if r + 1 == b {
                    Ok((m + 1, 0))
                } else {
                    Ok((m, r + 1))
                }
            }
            Err(s) => Err(s),
        }
    }
}

This code resembles one to one the proof above and it is not hard to adapt the code to also work for negative a, b, i.e. i32, i64, ....

More interesting though is the way how to provide an implementation that supports u32, u64, u128 at the same time. A technique how to accomplish this, can be found here.

You can run the code in this playground.

Thank you for reading!

Euclidean Division
Rust
Number Theory
Scientific Computing
Mathematics
Recommended from ReadMedium