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))
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))
def __hash__(self): """ Agrees with fractions.Fractions """ # XXX since this method is expensive, consider caching the result if self == int(self): return int(self) if self == float(self): return hash(float(self)) else: num = long(mpz._from_c_mpz(gmp.mpq_numref(self._mpq))) den = long(mpz._from_c_mpz(gmp.mpq_denref(self._mpq))) return hash((num, den))
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))
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)
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)
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)
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)
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)
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)
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)
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)
def __floor__(self): res = _new_mpz() gmp.mpz_fdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq)) return mpz._from_c_mpz(res)
def numerator(self): if self._numerator is None: num = _new_mpz() gmp.mpq_get_num(num, self._mpq) self._numerator = mpz._from_c_mpz(num) return self._numerator
def __long__(self): res = _new_mpz() gmp.mpz_tdiv_q(res, gmp.mpq_numref(self._mpq), gmp.mpq_denref(self._mpq)) return long(mpz._from_c_mpz(res))
def denominator(self): if self._denominator is None: den = _new_mpz() gmp.mpq_get_den(den, self._mpq) self._denominator = mpz._from_c_mpz(den) return self._denominator