pythagoras

    All the tools you need for integers and rational numbers.

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    20 hours ago
    Downloads
    3

    Dependencies

    kokic/pythagoras does not have a README file

    Factorization

    type Factorization[N, P] = (N) ->
    HashMap
    [N, P] raise InvokeError

    Abs

    pub trait Abs : HasNil + Neg + Compare {
    }

    impl Abs for Int
    impl Abs for BigInt

    HasNil

    pub trait HasNil {
    fn nil() -> Self
    }

    impl HasNil for Int

    HasOne

    pub trait HasOne {
    fn one() -> Self
    }

    impl HasOne for Int

    Signum

    pub trait Signum : HasNil + HasOne + Neg + Eq + Compare {
    }

    impl Signum for Int
    impl Signum for BigInt

    InvokeError

    pub suberror InvokeError {
    InvalidInput(msg~ : String)
    FactorizationFailed(remaining~ :
    BigInt
    )
    }

    abs

    fn[N : Abs + HasNil + Neg + Compare + Eq] abs(x : N) -> N

    cauchy_bound

    Parameters

    • coeffs : [a_0, a_1, ..., a_n] Raises InvalidInput for degree below one or a zero leading coefficient.

    divisors

    fn[E : Abs + HasOne + Mul + HasNil + Neg + Compare + Eq, P : HasOne + Compare + Add + Eq] divisors(n : E, f : (E) ->
    HashMap
    [E, P] raise InvokeError) -> Array[E] raise InvokeError

    All positive m such that m | n

    factor

    Returns a complete prime factorization, or raises if factor search fails.

    factor9973

    fn factor9973(n : Int) ->
    HashMap
    [Int, Int] raise InvokeError

    This function assumes that n has no prime factors greater than 9973 (i.e., n ≤ 9973² = 99460729). The returned result does not contain 1.

    Time complexity: (O(π(√n)) = O(√n/log n)), assuming arithmetic operations are (O(1)). Given the fixed input bound (n ≤ 9973²), the actual running time is bounded by a constant.

    factorial_naive

    fn[N : HasOne + Compare + Add + Mul + Eq] factorial_naive(n : N) -> N raise InvokeError

    gcd

    fn[N : Signum + Mod + HasNil + HasOne + Neg + Eq + Compare] gcd(a : N, b : N) -> N

    generate_time_based_seed

    fn generate_time_based_seed() -> Bytes

    is_root

    Parameters

    • coeffs : [a_0, a_1, ..., a_n]

    min

    fn[N : Compare + Eq] min(a : N, b : N) -> N

    pollard_rho_brent

    Pollard's Rho algorithm for integer factorization with Brent's cycle detection.

    Parameters

    • n: Composite number to be factored.
    • k: Frequency of GCD computations.
    • c: Constant used in the polynomial function.
    • f: Polynomial function (default is f(t) = (t^2 + c) mod n).
    • rand: Random number generator (default is a time-based seed).
    • retry: Number of attempts to find a non-trivial factor.

    pollard_rho_brent_loop

    Parameters

    • n: Composite number to be factored.
    • k: Frequency of GCD computations.
    • c: Constant used in the polynomial function.
    • f: Polynomial function (default is f(t) = (t^2 + c) mod n).
    • rand: Random number generator (default is a time-based seed).

    prime_factor_descent

    n ← min(a, n/a) where a is a non-trivial factor of n found via Pollard's rho.

    random_time_based

    fn random_time_based() ->
    Rand

    rational_root_candidates

    Parameters

    • coeffs : [a_0, a_1, ..., a_n] High-degree zero coefficients are ignored. Nonzero constants have no candidates. Empty or all-zero coefficients raise InvalidInput, since the zero polynomial has infinitely many rational roots. Factor search failure is propagated rather than returning incomplete candidates.

    signum

    fn[N : Signum + HasNil + HasOne + Neg + Eq + Compare, S : HasNil + HasOne + Neg] signum(a : N) -> S

    sorted_factors

    fn[E : Compare + Eq, P : Compare + Eq] sorted_factors(n : E, f : (E) ->
    HashMap
    [E, P] raise InvokeError) -> Array[(E, P)] raise InvokeError

    uint64_array_to_bytes

    fn uint64_array_to_bytes(values : Array[UInt64]) -> Bytes