User:Inthar/Code: Difference between revisions

Inthar (talk | contribs)
No edit summary
Inthar (talk | contribs)
Line 931: Line 931:
use std::io::Write;
use std::io::Write;
use std::time::Instant;
use std::time::Instant;
/// Gives the greatest common denominator of the two inputs, unless that's 2^63.
/// 2^63 doesn't fit in an `i64`, so it returns -2^63, which does.
pub fn gcd(u: i64, v: i64) -> i64 {
    // `wrapping_abs` gives a number's absolute value, unless that's 2^63. 2^63
    // won't fit in `i64`, so it gives -2^63 instead.
    let mut v = v.wrapping_abs() as u64;
    if u == 0 {
        return v as i64;
    }
    let mut u = u.wrapping_abs() as u64;
    if v == 0 {
        return u as i64;
    }
    // `|` is bitwise OR. `trailing_zeros` quickly counts a binary number's
    // trailing zeros, giving its prime factorization's exponent on two.
    let gcd_exponent_on_two = (u | v).trailing_zeros();
    // `>>=` divides the left by two to the power of the right, storing that in
    // the left variable. `u` divided by its prime factorization's power of two
    // turns it odd.
    u >>= u.trailing_zeros();
    v >>= v.trailing_zeros();
    while u != v {
        if u < v {
            // Swap the variables' values with each other.
            core::mem::swap(&mut u, &mut v);
        }
        u -= v;
        u >>= u.trailing_zeros();
    }
    // `<<` multiplies the left by two to the power of the right.
    (u << gcd_exponent_on_two) as i64
}


fn main() -> std::io::Result<()> {
fn main() -> std::io::Result<()> {
Line 976: Line 939:
         for b in 1..=a {
         for b in 1..=a {
             for c in 1..=b {
             for c in 1..=b {
                 if a + b + c <= 31 && gcd(gcd(a as i64, b as i64), c as i64) == 1 {
                 if a + b + c <= 31 && scale_theory::gcd(scale_theory::gcd(a as i64, b as i64), c as i64) == 1 {
                     writeln!(&f, "== {}x{}y{}z ==", a, b, c);
                     writeln!(&f, "== {}x{}y{}z ==", a, b, c);
                     let billiard_scales = scale_theory::billiard_scales(a, b, c);
                     let billiard_scales = scale_theory::billiard_scales(a, b, c);
Line 992: Line 955:
}
}
</syntaxhighlight>
</syntaxhighlight>
=== Cargo.toml ===
=== Cargo.toml ===
<syntaxhighlight lang="toml">
<syntaxhighlight lang="toml">