Ejemplo n.º 1
0
def fib2(n):
    """
    fib2(n) -> tuple

    Return a 2-tuple with the (n-1)-th and n-th Fibonacci numbers.
    """
    n = _check_int('fib2', 'n', n)
    if n < 0:
        raise ValueError('Fibonacci of negative number')
    res, res1 = _new_mpz(), _new_mpz()
    gmp.mpz_fib2_ui(res, res1, n)
    return (mpz._from_c_mpz(res), mpz._from_c_mpz(res1))
Ejemplo n.º 2
0
def lucas2(n):
    """
    lucas2(n) -> tuple

    Return a 2-tuple with the (n-1)-th and n-th Lucas numbers.
    """
    n = _check_int('lucas2', 'n', n)
    if n < 0:
        raise ValueError('Lucas of negative number')
    res, res1 = _new_mpz(), _new_mpz()
    gmp.mpz_lucnum2_ui(res, res1, n)
    return (mpz._from_c_mpz(res), mpz._from_c_mpz(res1))
Ejemplo n.º 3
0
def fib2(n):
    """
    fib2(n) -> tuple

    Return a 2-tuple with the (n-1)-th and n-th Fibonacci numbers.
    """
    n = _check_int('fib2', 'n', n)
    if n < 0:
        raise ValueError('Fibonacci of negative number')
    res, res1 = _new_mpz(), _new_mpz()
    gmp.mpz_fib2_ui(res, res1, n)
    return (mpz._from_c_mpz(res), mpz._from_c_mpz(res1))
Ejemplo n.º 4
0
def lucas2(n):
    """
    lucas2(n) -> tuple

    Return a 2-tuple with the (n-1)-th and n-th Lucas numbers.
    """
    n = _check_int('lucas2', 'n', n)
    if n < 0:
        raise ValueError('Lucas of negative number')
    res, res1 = _new_mpz(), _new_mpz()
    gmp.mpz_lucnum2_ui(res, res1, n)
    return (mpz._from_c_mpz(res), mpz._from_c_mpz(res1))
Ejemplo n.º 5
0
def gcdext(a, b):
    """
    gcdext(a, b) - > tuple

    Return a 3-element tuple (g,s,t) such that
        g == gcd(a,b) and g == a*s + b*t
    """
    a = _check_mpz('gcdext', 'a', a)
    b = _check_mpz('gcdext', 'b', b)
    mpz_g, mpz_s, mpz_t = _new_mpz(), _new_mpz(), _new_mpz()
    gmp.mpz_gcdext(mpz_g, mpz_s, mpz_t, a._mpz, b._mpz)
    return (mpz._from_c_mpz(mpz_g), mpz._from_c_mpz(mpz_s),
            mpz._from_c_mpz(mpz_t))
Ejemplo n.º 6
0
def gcdext(a, b):
    """
    gcdext(a, b) - > tuple

    Return a 3-element tuple (g,s,t) such that
        g == gcd(a,b) and g == a*s + b*t
    """
    a = _check_mpz('gcdext', 'a', a)
    b = _check_mpz('gcdext', 'b', b)
    mpz_g, mpz_s, mpz_t = _new_mpz(), _new_mpz(), _new_mpz()
    gmp.mpz_gcdext(mpz_g, mpz_s, mpz_t, a._mpz, b._mpz)
    return (mpz._from_c_mpz(mpz_g),
            mpz._from_c_mpz(mpz_s),
            mpz._from_c_mpz(mpz_t))
Ejemplo n.º 7
0
def next_prime(x):
    """
    next_prime(x) -> mpz

    Return the next _probable_ prime number > x.
    """
    x = _check_mpz('next_prime', 'x', x)
    res = _new_mpz()
    gmp.mpz_nextprime(res, x._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 8
0
def next_prime(x):
    """
    next_prime(x) -> mpz

    Return the next _probable_ prime number > x.
    """
    x = _check_mpz('next_prime', 'x', x)
    res = _new_mpz()
    gmp.mpz_nextprime(res, x._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 9
0
def gcd(a, b):
    """
    gcd(a, b) -> mpz

    Return the greatest common denominator of integers a and b.
    """
    a = _check_mpz('gcd', 'a', a)
    b = _check_mpz('gcd', 'b', b)
    res = _new_mpz()
    gmp.mpz_gcd(res, a._mpz, b._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 10
0
def lcm(a, b):
    """
    lcm(a, b) -> mpz

    Return the lowest common multiple of integers a and b.
    """
    a = _check_mpz('lcm', 'a', a)
    b = _check_mpz('lcm', 'b', b)
    res = _new_mpz()
    gmp.mpz_lcm(res, a._mpz, b._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 11
0
def gcd(a, b):
    """
    gcd(a, b) -> mpz

    Return the greatest common denominator of integers a and b.
    """
    a = _check_mpz('gcd', 'a', a)
    b = _check_mpz('gcd', 'b', b)
    res = _new_mpz()
    gmp.mpz_gcd(res, a._mpz, b._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 12
0
def lcm(a, b):
    """
    lcm(a, b) -> mpz

    Return the lowest common multiple of integers a and b.
    """
    a = _check_mpz('lcm', 'a', a)
    b = _check_mpz('lcm', 'b', b)
    res = _new_mpz()
    gmp.mpz_lcm(res, a._mpz, b._mpz)
    return mpz._from_c_mpz(res)
Ejemplo n.º 13
0
def fib(n):
    """
    fib(n) -> mpz

    Return the n-th Fibonacci number.
    """
    n = _check_int('fib', 'n', n)
    if n < 0:
        raise ValueError('Fibonacci of negative number')
    res = _new_mpz()
    gmp.mpz_fib_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 14
0
def lucas(n):
    """
    lucas(n) -> mpz

    Return the n-th Lucas number.
    """
    n = _check_int('lucas', 'n', n)
    if n < 0:
        raise ValueError('Lucas of negative number')
    res = _new_mpz()
    gmp.mpz_lucnum_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 15
0
def fib(n):
    """
    fib(n) -> mpz

    Return the n-th Fibonacci number.
    """
    n = _check_int('fib', 'n', n)
    if n < 0:
        raise ValueError('Fibonacci of negative number')
    res = _new_mpz()
    gmp.mpz_fib_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 16
0
def lucas(n):
    """
    lucas(n) -> mpz

    Return the n-th Lucas number.
    """
    n = _check_int('lucas', 'n', n)
    if n < 0:
        raise ValueError('Lucas of negative number')
    res = _new_mpz()
    gmp.mpz_lucnum_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 17
0
def invert(x, m):
    """
    invert(x, m) -> mpz

    Return y such that x*y == 1 (mod m). Raises ZeroDivisionError if no
    inverse exists.
    """
    x = _check_mpz('invert', 'x', x)
    m = _check_mpz('invert', 'm', m)
    res = _new_mpz()
    if gmp.mpz_invert(res, x._mpz, m._mpz) == 0:
        raise ZeroDivisionError
    return mpz._from_c_mpz(res)
Ejemplo n.º 18
0
def invert(x, m):
    """
    invert(x, m) -> mpz

    Return y such that x*y == 1 (mod m). Raises ZeroDivisionError if no
    inverse exists.
    """
    x = _check_mpz('invert', 'x', x)
    m = _check_mpz('invert', 'm', m)
    res = _new_mpz()
    if gmp.mpz_invert(res, x._mpz, m._mpz) == 0:
        raise ZeroDivisionError
    return mpz._from_c_mpz(res)
Ejemplo n.º 19
0
def fac(n):
    """
    fac(n) -> mpz

    Return the exact factorial of n.

    See factorial(n) to get the floating-point approximation.
    """
    n = _check_int('fac', 'n', n)
    if n < 0:
        raise ValueError('fac() of negative number')
    res = _new_mpz()
    gmp.mpz_fac_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 20
0
def fac(n):
    """
    fac(n) -> mpz

    Return the exact factorial of n.

    See factorial(n) to get the floating-point approximation.
    """
    n = _check_int('fac', 'n', n)
    if n < 0:
        raise ValueError('fac() of negative number')
    res = _new_mpz()
    gmp.mpz_fac_ui(res, n)
    return mpz._from_c_mpz(res)
Ejemplo n.º 21
0
def bincoef(x, n):
    """
    bincoef(x, n) -> mpz

    Return the binomial coefficient ('x over n'). n >= 0.
    """
    n = _check_int('bincoef', 'n', n)
    if n < 0:
        raise ValueError('binomial coefficient with negative k')
    res = _new_mpz()
    if isinstance(x, mpz):
        gmp.mpz_bin_ui(res, x._mpz, n)
    elif isinstance(x, (int, long)) and -sys.maxsize - 1 <= x <= sys.maxsize:
        gmp.mpz_bin_uiui(res, x, n)
    else:
        raise TypeError
    return mpz._from_c_mpz(res)
Ejemplo n.º 22
0
def bincoef(x, n):
    """
    bincoef(x, n) -> mpz

    Return the binomial coefficient ('x over n'). n >= 0.
    """
    n = _check_int('bincoef', 'n', n)
    if n < 0:
        raise ValueError('binomial coefficient with negative k')
    res = _new_mpz()
    if isinstance(x, mpz):
        gmp.mpz_bin_ui(res, x._mpz, n)
    elif isinstance(x, (int, long)) and -sys.maxsize - 1 <= x <= sys.maxsize:
        gmp.mpz_bin_uiui(res, x, n)
    else:
        raise TypeError
    return mpz._from_c_mpz(res)