math/integer/module.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
57f4f924e3ffe6f87c3e4ada0327ff1f6ad192f7
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Exact mathematical algorithms over ordinary unbounded Core `Integer` values.

/// Returns the non-negative greatest common divisor of two ordinary Integers.
this.gcd: (a, b) => {
    // Validate the exact ordinary-Integer family without conversion or delegation.
    Integer.recognizes(a).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(b).ifFalse(() => {
        Error().signal()
    })

    left: a
    right: b

    if(left < 0) {
        left = 0 - left
    }
    if(right < 0) {
        right = 0 - right
    }

    while(right != 0) {
        remainder: left.mod(right)
        left = right
        right = remainder
    }

    left
}

/// Returns the non-negative least common multiple of two ordinary Integers.
this.lcm: (a, b) => {
    // gcd validates both arguments before any zero short-circuit.
    divisor: gcd(a, b)
    result: 0

    (a == 0).ifFalse(() => {
        (b == 0).ifFalse(() => {
            // Divide first so the implementation does not construct the
            // avoidable full a*b intermediate before removing the gcd factor.
            value: a.div(divisor) * b
            if(value < 0) {
                value = 0 - value
            }
            result = value
        })
    })

    result
}

/// Returns the exact factorial of a non-negative ordinary Integer.
this.factorial: (n) => {
    // Preserve the strict ordinary-Integer family boundary without conversion.
    Integer.recognizes(n).ifFalse(() => {
        Error().signal()
    })

    if(n < 0) {
        Error().signal()
    }

    // A balanced product tree keeps recursion depth logarithmic and avoids the
    // permanently sequential multiplication shape rejected by LIB007-0.
    productRange: (low, high) => {
        result: 1

        if(low <= high) {
            if(low == high) {
                result = low
            } else {
                middle: low + (high - low).div(2)
                result =
                    productRange(low, middle) *
                    productRange(middle + 1, high)
            }
        }

        result
    }

    productRange(2, n)
}

/// Returns the exact non-negative-integer power using exponentiation by squaring.
this.pow: (base, exponent) => {
    Integer.recognizes(base).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(exponent).ifFalse(() => {
        Error().signal()
    })

    if(exponent < 0) {
        Error().signal()
    }

    result: 1
    factor: base
    remaining: exponent

    while(remaining != 0) {
        if(remaining.mod(2) == 1) {
            result = result * factor
        }

        remaining = remaining.div(2)

        if(remaining != 0) {
            factor = factor * factor
        }
    }

    result
}

/// Returns canonical mathematical modular exponentiation for a positive modulus.
this.powMod: (base, exponent, modulus) => {
    Integer.recognizes(base).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(exponent).ifFalse(() => {
        Error().signal()
    })
    Integer.recognizes(modulus).ifFalse(() => {
        Error().signal()
    })

    if(exponent < 0) {
        Error().signal()
    }
    if(modulus <= 0) {
        Error().signal()
    }

    canonicalMod: (value) => {
        remainder: value.mod(modulus)
        if(remainder < 0) {
            remainder = remainder + modulus
        }
        remainder
    }

    result: canonicalMod(1)
    factor: canonicalMod(base)
    remaining: exponent

    while(remaining != 0) {
        if(remaining.mod(2) == 1) {
            result = canonicalMod(result * factor)
        }

        remaining = remaining.div(2)

        if(remaining != 0) {
            factor = canonicalMod(factor * factor)
        }
    }

    result
}