Class: GMP::Z

Inherits:
Integer
  • Object
show all
Defined in:
ext/gmpz.c,
ext/gmp.c,
ext/gmpz.c

Overview

GMP Multiple Precision Integer.

Instances of this class can store variables of the type mpz_t. This class also contains many methods that act as the functions for mpz_t variables, as well as a few methods that attempt to make this library more Ruby-ish.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



498
499
500
501
502
503
504
505
506
507
# File 'ext/gmpz.c', line 498

VALUE r_gmpz_initialize(int argc, VALUE *argv, VALUE self)
{
  MP_INT *self_val;

  if (argc != 0) {
    mpz_get_struct(self,self_val);
    mpz_set_value (self_val, argv[0]);
  }
  return Qnil;
}

Class Method Details

.absObject

call-seq:

a.abs

Returns the absolute value of a.

.addObject

call-seq:

GMP::Z.add(rop, op1, op2)

.addmulObject

.cdiv_q_2expObject

.cdiv_r_2expObject

.comObject

call-seq:

a.com

Returns the one’s complement of a.

.divexactObject

Functional Mappings

.facObject

call-seq:

GMP::Z.fac(n)

Returns n!, the factorial of n.

Examples:

  • GMP::Z.fac(0) #=> 1

  • GMP::Z.fac(1) #=> 1

  • GMP::Z.fac(2) #=> 2

  • GMP::Z.fac(3) #=> 6

  • GMP::Z.fac(4) #=> 24

.fdiv_q_2expObject

.fdiv_r_2expObject

.fibObject

call-seq:

GMP::Z.fib(n)

Returns F[n], the nth Fibonacci number.

Examples:

  • GMP::Z.fib(1) #=> 1

  • GMP::Z.fib(2) #=> 1

  • GMP::Z.fib(3) #=> 2

  • GMP::Z.fib(4) #=> 3

  • GMP::Z.fib(5) #=> 5

  • GMP::Z.fib(6) #=> 8

  • GMP::Z.fib(7) #=> 13

.GMP::Z.jacobi(a, b) ⇒ Object

Calculate the Jacobi symbol (a/b). This is defined only for b odd and positive.



1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
# File 'ext/gmpz.c', line 1734

VALUE r_gmpzsg_jacobi(VALUE klass, VALUE a, VALUE b)
{
  MP_INT *a_val, *b_val;
  int res_val;
  int free_a_val = 0;
  int free_b_val = 0;
  (void)klass;
  
  if (GMPZ_P(a)) {
    mpz_get_struct(a, a_val);
  } else if (FIXNUM_P(a)) {
    mpz_temp_alloc(a_val);
    mpz_init_set_ui(a_val, FIX2NUM(a));
    free_a_val = 1;
  } else if (BIGNUM_P(a)) {
    mpz_temp_from_bignum(a_val, a);
    free_a_val = 1;
  } else {
    typeerror_as(ZXB, "a");
  }
  
  if (GMPZ_P(b)) {
    mpz_get_struct(b, b_val);
    if (mpz_sgn(b_val) != 1)
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
    if (mpz_even_p(b_val))
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
  } else if (FIXNUM_P(b)) {
    if (FIX2NUM(b) <= 0)
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
    if (FIX2NUM(b) % 2 == 0)
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
    mpz_temp_alloc(b_val);
    mpz_init_set_ui(b_val, FIX2NUM(b));
    free_b_val = 1;
  } else if (BIGNUM_P(b)) {
    mpz_temp_from_bignum(b_val, b);
    if (mpz_sgn(b_val) != 1) {
      mpz_temp_free(b_val);
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
    }
    if (mpz_even_p(b_val)) {
      mpz_temp_free(b_val);
      rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
    }
    free_b_val = 1;
  } else {
    typeerror_as(ZXB, "b");
  }
  
  res_val = mpz_jacobi(a_val, b_val);
  if (free_a_val) { mpz_temp_free(a_val); }
  if (free_b_val) { mpz_temp_free(b_val); }
  return INT2FIX(res_val);
}

.lcmObject

Functional Mappings

.mulObject

.mul_2expObject

.negObject

call-seq:

a.neg
-a

Returns -a.

.newObject

Initializing, Assigning Integers

.nextprimeObject

call-seq:

n.nextprime
n.next_prime

Returns the next prime greater than n.

This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.

.powObject

call-seq:

GMP::Z.pow(a, b)

Returns a raised to b. The case 0^0 yields 1.

.sqrtObject

call-seq:

a.sqrt

Returns the truncated integer part of the square root of a.

.subObject

.submulObject

.tdiv_q_2expObject

.tdiv_r_2expObject

Instance Method Details

#%Object

#&Object

call-seq:

a & b

Returns a bitwise-and b. b must be an instance of one of the following:

  • GMP::Z

  • Fixnum

  • Bignum

#*(b) ⇒ Object

Multiplies a with b. a must be an instance of one of

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
# File 'ext/gmpz.c', line 886

VALUE r_gmpz_mul(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val, *res_val;
  VALUE res;

  mpz_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_get_struct(arg,arg_val);
    mpz_mul(res_val, self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_mul_si(res_val, self_val, FIX2NUM(arg));
  } else if (GMPQ_P(arg)) {
    return r_gmpq_mul(arg, self);
  } else if (GMPF_P(arg)) {
#ifndef MPFR
    return r_gmpf_mul(arg, self);
#else
    return rb_funcall(arg, rb_intern("*"), 1, self);
#endif
  } else if (BIGNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_set_bignum(res_val, arg);
    mpz_mul(res_val, res_val, self_val);
  } else {
    typeerror(ZQFXB);
  }
  return res;
}

#**Object

call-seq:

a ** b

Returns a raised to b. The case 0^0 yields 1.

#+(b) ⇒ Object

Adds a to b. b must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
# File 'ext/gmpz.c', line 716

VALUE r_gmpz_add(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val, *res_val;
  VALUE res;

  mpz_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val);
    mpz_make_struct_init(res, res_val);
    mpz_add(res_val, self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    if (FIX2NUM(arg) > 0)
      mpz_add_ui(res_val, self_val, FIX2NUM(arg));
    else
      mpz_sub_ui(res_val, self_val, -FIX2NUM(arg));
  } else if (GMPQ_P(arg)) {
    return r_gmpq_add(arg, self);
  } else if (GMPF_P(arg)) {
#ifndef MPFR
    return r_gmpf_add(arg, self);
#else
    return rb_funcall(arg, rb_intern("+"), 1, self);
#endif
  } else if (BIGNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_init(res_val);
    mpz_set_bignum(res_val, arg);
    mpz_add(res_val, res_val, self_val);
  } else {
    typeerror(ZQFXB);
  }
  return res;
}

#-(b) ⇒ Object

Subtracts b from a. b must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
# File 'ext/gmpz.c', line 798

VALUE r_gmpz_sub(VALUE self, VALUE arg)
{
  MP_RAT *res_val_q, *arg_val_q;
  MP_INT *self_val, *arg_val, *res_val;
  MP_FLOAT *arg_val_f, *res_val_f;
  VALUE res;
  unsigned long prec;

  mpz_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_get_struct(arg,arg_val);
    mpz_sub (res_val, self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    if (FIX2NUM(arg) > 0)
      mpz_sub_ui (res_val, self_val, FIX2NUM(arg));
    else
      mpz_add_ui (res_val, self_val, -FIX2NUM(arg));
  } else if (GMPQ_P(arg)) {
    mpq_make_struct_init(res, res_val_q);
    mpq_get_struct(arg,arg_val_q);
    mpz_set (mpq_denref(res_val_q), mpq_denref(arg_val_q));
    mpz_mul (mpq_numref(res_val_q), mpq_denref(arg_val_q), self_val);
    mpz_sub (mpq_numref(res_val_q), mpq_numref(res_val_q), mpq_numref(arg_val_q));
  } else if (GMPF_P(arg)) {
    mpf_get_struct_prec (arg, arg_val_f, prec);
    mpf_make_struct_init(res, res_val_f, prec);
    mpf_set_z (res_val_f, self_val);
    mpf_sub (res_val_f, res_val_f, arg_val_f);
  } else if (BIGNUM_P(arg)) {
    mpz_make_struct_init(res, res_val);
    mpz_set_bignum (res_val, arg);
    mpz_sub (res_val, self_val, res_val);
  } else {
    typeerror (ZQFXB);
  }
  return res;
}

#-@Object

#/Object

Integer Division

#<Object

call-seq:

a < b

Returns whether a is strictly less than b.

#<<Object

call-seq:

a << n

Returns a times 2 raised to n. This operation can also be defined as a left shift by n bits.

#<=Object

call-seq:

a <= b

Returns whether a is less than or equal to b.

#<=>(b) ⇒ Object

Returns negative if a is less than b.

Returns 0 if a is equal to b.

Returns positive if a is greater than b.



1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
# File 'ext/gmpz.c', line 1978

VALUE r_gmpz_cmp(VALUE self, VALUE arg)
{
  MP_INT *self_val;
  int res;
  mpz_get_struct(self,self_val);
  res = mpz_cmp_value(self_val, arg);
  if (res > 0)
    return INT2FIX(1);
  else if (res == 0)
    return INT2FIX(0);
  else
    return INT2FIX(-1);
}

#==Object

#>Object

call-seq:

a > b

Returns whether a is strictly greater than b.

#>=Object

call-seq:

a >= b

Returns whether a is greater than or equal to b.

#>>Object

unsorted

#[](index) ⇒ Object

Gets the bit at index, returned as either true or false.



2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
# File 'ext/gmpz.c', line 2296

VALUE r_gmpz_getbit(VALUE self, VALUE bitnr)
{
  MP_INT *self_val;
  unsigned long bitnr_val;
  mpz_get_struct(self, self_val);
  if (FIXNUM_P(bitnr)) {
    bitnr_val = FIX2NUM (bitnr);
  } else {
    typeerror_as(X, "index");
  }
  return mpz_tstbit(self_val, bitnr_val)?Qtrue:Qfalse;
}

#[]=(index) ⇒ Object

Sets the bit at index to x.



2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
# File 'ext/gmpz.c', line 2267

VALUE r_gmpz_setbit(VALUE self, VALUE bitnr, VALUE set_to)
{
  MP_INT *self_val;
  unsigned long bitnr_val;

  mpz_get_struct (self, self_val);

  if (FIXNUM_P (bitnr)) {
    if (FIX2NUM (bitnr) < 0) {
      rb_raise(rb_eRangeError, "index must be nonnegative");
    }
    bitnr_val = FIX2NUM (bitnr);
  } else {
    typeerror_as (X, "index");
  }
  if (RTEST (set_to)) {
    mpz_setbit (self_val, bitnr_val);
  } else {
    mpz_clrbit (self_val, bitnr_val);
  }
  return Qnil;
}

#^Object

call-seq:

a ^ b

Returns a bitwise exclusive-or b. b must be an instance of one of the following:

  • GMP::Z

  • Fixnum

  • Bignum

#absObject

call-seq:

a.abs

Returns the absolute value of a.

#abs!Object

call-seq:

a.abs!

Sets a to its absolute value.

#add!(_b_) ⇒ Object

Adds a to b in-place, setting a to the sum. b must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
# File 'ext/gmpz.c', line 763

VALUE r_gmpz_add_self(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val;

  mpz_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val);
    mpz_add(self_val, self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    if (FIX2NUM(arg) > 0)
      mpz_add_ui(self_val, self_val, FIX2NUM(arg));
    else
      mpz_sub_ui(self_val, self_val, -FIX2NUM(arg));
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    mpz_add(self_val, self_val, arg_val);
    mpz_temp_free(arg_val);
  } else {
    typeerror(ZXB);
  }
  return Qnil;
}

#addmul!(b, c) ⇒ Object

Sets a to a plus b times c. b and c must each be an instance of one of

  • GMP::Z

  • Fixnum

  • Bignum

Since:

  • 0.4.19



929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
# File 'ext/gmpz.c', line 929

static VALUE r_gmpz_addmul_self(VALUE self, VALUE b, VALUE c)
{
  MP_INT *self_val, *b_val, *c_val;
  int free_b_val = 0;

  if (GMPZ_P(b)) {
    mpz_get_struct(b, b_val);
  } else if (FIXNUM_P(b)) {
    mpz_temp_alloc(b_val);
    mpz_init_set_si(b_val, FIX2NUM(b));
    free_b_val = 1;
  } else if (BIGNUM_P(b)) {
    mpz_temp_from_bignum(b_val, b);
    free_b_val = 1;
  } else {
    typeerror_as(ZXB, "addend");
  }
  mpz_get_struct(self, self_val);

  if (GMPZ_P(c)) {
    mpz_get_struct(c, c_val);
    mpz_addmul(self_val, b_val, c_val);
  } else if (FIXNUM_P(c)) {
    if (FIX2NUM(c) < 0)
    {
      if (free_b_val) { mpz_temp_free(b_val); }
      rb_raise(rb_eRangeError, "multiplicand (Fixnum) must be nonnegative");
    }
    mpz_addmul_ui(self_val, b_val, FIX2NUM(c));
  } else if (BIGNUM_P(c)) {
    mpz_temp_from_bignum(c_val, c);
    mpz_addmul(self_val, b_val, c_val);
    mpz_temp_free(c_val);
  } else {
    if (free_b_val)
      mpz_temp_free(b_val);
    typeerror_as(ZXB, "multiplicand");
  }
  if (free_b_val)
    mpz_temp_free(b_val);
  return self;
}

#cdivObject

call-seq:

n.cdiv(d)

Divide n by d, forming a quotient q. cdiv rounds q up towards _+infinity_. The c stands for “ceil”.

q will satisfy n=q*d+r.

This function calculates only the quotient.

#cmodObject

call-seq:

n.cmod(d)

Divides n by d, forming a remainder r. r will have the opposite sign of d. The c stands for “ceil”.

r will satisfy n=q*d+r, and r will satisfy 0 <= abs( r ) < abs( d ).

This function calculates only the remainder.

#cmpabsObject

#coerce(arg) ⇒ Object



65
66
67
68
# File 'ext/gmp.c', line 65

static VALUE r_gmpz_coerce(VALUE self, VALUE arg)
{
  return rb_assoc_new(r_gmpzsg_new(1, &arg, cGMP_Z), self);
}

#comObject

call-seq:

a.com

Returns the one’s complement of a.

#com!Object

call-seq:

a.com!

Sets a to its one’s complement.

#divisible?(b) ⇒ Boolean

Returns true if a is divisible by b. b can be an instance any of the following:

  • GMP::Z

  • Fixnum

  • Bignum

Returns:

  • (Boolean)

Since:

  • 0.5.23



1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
# File 'ext/gmpz.c', line 1305

static VALUE r_gmpz_divisible(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val;
  int res;
  mpz_get_struct (self, self_val);
  
  if (FIXNUM_P (arg) && FIX2NUM (arg) > 0) {
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2NUM(arg));
    res = mpz_divisible_ui_p (self_val, FIX2NUM (arg));
    mpz_temp_free(arg_val);
  } else if (FIXNUM_P (arg)) {
    mpz_temp_alloc(arg_val);
    mpz_make_struct_init (arg, arg_val);
    mpz_init_set_si(arg_val, FIX2NUM(arg));
    res = mpz_divisible_p (self_val, arg_val);
    mpz_temp_free(arg_val);
  } else if (BIGNUM_P (arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    res = mpz_divisible_p (self_val, arg_val);
    mpz_temp_free(arg_val);
  } else if (GMPZ_P (arg)) {
    mpz_get_struct(arg, arg_val);
    res = mpz_divisible_p (self_val, arg_val);
  } else {
    typeerror_as (ZXB, "argument");
  }
  return (res != 0) ? Qtrue : Qfalse;
}

#eql?(b) ⇒ Boolean

Returns true if a is equal to b. a and b must then be equal in cardinality, and both be instances of GMP::Z. Otherwise, returns false. a.eql?(b) if and only if a.hash == b.hash.

Returns:

  • (Boolean)

Since:

  • 0.4.7



2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
# File 'ext/gmpz.c', line 2098

VALUE r_gmpz_eql(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val;
  mpz_get_struct(self,self_val);
  
  if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val);
    return (mpz_cmp (self_val, arg_val) == 0) ? Qtrue : Qfalse;
  }
  else {
    return Qfalse;
  }
}

#even?Boolean

call-seq:

a.even?

Determines whether a is even. Returns true or false.

Returns:

  • (Boolean)

#fdivObject

call-seq:

n.fdiv(d)

Divide n by d, forming a quotient q. fdiv rounds q down towards -infinity. The f stands for “floor”.

q will satisfy n=q*d+r.

This function calculates only the quotient.

#fmodObject

call-seq:

n.fmod(d)

Divides n by d, forming a remainder r. r will have the same sign as d. The f stands for “floor”.

r will satisfy n=q*d+r, and r will satisfy 0 <= abs( r ) < abs( d ).

This function calculates only the remainder.

The remainder can be negative, so the return value is the absolute value of the remainder.

#gcdObject

#gcdext(b) ⇒ Object

Returns the greatest common divisor of a and b, in addition to s and t, the coefficients satisfying a*s + b*t = g. g is always positive, even if one or both of a and b are negative. s and t are chosen such that abs(s) <= abs(b) and abs(t) <= abs(a).

Since:

  • 0.5.23



1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
# File 'ext/gmpz.c', line 1631

VALUE r_gmpz_gcdext(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val, *res_val, *s_val, *t_val;
  VALUE res, s, t, ary;
  int free_arg_val = 0;

  mpz_get_struct (self,self_val);

  if (GMPZ_P (arg)) {
    mpz_make_struct_init (res, res_val);
    mpz_make_struct_init (s, s_val);
    mpz_make_struct_init (t, t_val);
    mpz_get_struct (arg, arg_val);
    mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
  } else if (FIXNUM_P (arg)) {
    mpz_make_struct_init (res, res_val);
    mpz_make_struct_init (s, s_val);
    mpz_make_struct_init (t, t_val);
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2NUM(arg));
    free_arg_val = 1;
    mpz_gcdext (res_val, s_val, t_val, self_val, arg_val);
  } else if (BIGNUM_P (arg)) {
    mpz_make_struct_init (res, res_val);
    mpz_make_struct_init (s, s_val);
    mpz_make_struct_init (t, t_val);
    mpz_set_bignum (res_val, arg);
    mpz_gcdext (res_val, s_val, t_val, res_val, self_val);
  } else {
    typeerror (ZXB);
  }
  
  if (free_arg_val)
    mpz_temp_free(arg_val);
  
  ary = rb_ary_new3(3, res, s, t);
  return ary;
}

#hashObject

Returns the computed hash value of a. This method first converts a into a String (base 10), then calls String#hash on the result, returning the hash value. a.eql?(b) if and only if a.hash == b.hash.

Since:

  • 0.4.7



2122
2123
2124
2125
2126
2127
# File 'ext/gmpz.c', line 2122

VALUE r_gmpz_hash(VALUE self)
{
  ID to_s_sym = rb_intern("to_s");
  ID hash_sym = rb_intern("hash");
  return rb_funcall(rb_funcall(self, to_s_sym, 0), hash_sym, 0);
}

#invert(b) ⇒ Object

Returns the inverse of a modulo b. If the inverse exists, the return value is non-zero and the result will be non-negative and less than b. If an inverse doesn’t exist, the result is undefined.

Since:

  • 0.2.11



1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
# File 'ext/gmpz.c', line 1680

VALUE r_gmpz_invert(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val, *res_val;
  VALUE res;

  mpz_get_struct (self,self_val);

  if (GMPZ_P (arg)) {
    mpz_make_struct_init (res, res_val);
    mpz_get_struct (arg, arg_val);
    mpz_invert (res_val, self_val, arg_val);
  } else if (FIXNUM_P (arg)) {
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2NUM(arg));
    mpz_make_struct_init (res, res_val);
    mpz_invert (res_val, self_val, arg_val);
  } else if (BIGNUM_P (arg)) {
    mpz_make_struct_init (res, res_val);
    mpz_set_bignum (res_val, arg);
    mpz_invert (res_val, res_val, self_val);
  } else {
    typeerror (ZXB);
  }
  return res;
}

#jacobi(b) ⇒ Object

Calculate the Jacobi symbol (a/b). This is defined only for b odd and positive.



1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
# File 'ext/gmpz.c', line 1713

VALUE r_gmpz_jacobi(VALUE self, VALUE b)
{
  MP_INT *self_val, *b_val;
  int res_val;
  mpz_get_struct(self, self_val);
  mpz_get_struct(   b,    b_val);
  if (mpz_sgn(b_val) != 1)
    rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is non-positive.");
  if (mpz_even_p(b_val))
    rb_raise(rb_eRangeError, "Cannot take Jacobi symbol (a/b) where b is even.");
  res_val = mpz_jacobi(self_val, b_val);
  return INT2FIX(res_val);
}

#lastbits_posObject

#lastbits_sgnObject

#legendre(p) ⇒ Object

Calculate the Legendre symbol (a/p). This is defined only for p an odd positive prime, and for such p it’s identical to the Jacobi symbol.



1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
# File 'ext/gmpz.c', line 1797

VALUE r_gmpz_legendre(VALUE self, VALUE p)
{
  MP_INT *self_val, *p_val;
  int res_val;
  mpz_get_struct(self, self_val);
  mpz_get_struct(   p,    p_val);
  if (mpz_sgn(p_val) != 1)
    rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is non-positive.");
  if (mpz_even_p(p_val))
    rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is even.");
  if (mpz_probab_prime_p(p_val, 5) == 0)
    rb_raise(rb_eRangeError, "Cannot take Legendre symbol (a/p) where p is composite.");
  res_val = mpz_legendre(self_val, p_val);
  return INT2FIX(res_val);
}

#negObject

call-seq:

a.neg
-a

Returns -a.

#neg!Object

call-seq:

a.neg!

Sets a to -a.

#nextprimeObject Also known as: next_prime

call-seq:

n.nextprime
n.next_prime

Returns the next prime greater than n.

This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.

#nextprime!Object Also known as: next_prime!

call-seq:

n.nextprime!
n.next_prime!

Sets n to the next prime greater than n.

This function uses a probabilistic algorithm to identify primes. For practical purposes it’s adequate, the chance of a composite passing will be extremely small.

#odd?Boolean

call-seq:

a.odd?

Determines whether a is odd. Returns true or false.

Returns:

  • (Boolean)

#popcountObject

call-seq:

a.popcount

If a >= 0, return the population count of a, which is the number of 1 bits in the binary representation. If a < 0, the number of 1s is infinite, and the return value is INT2FIX(ULONG_MAX), the largest possible unsigned long.

#power?Boolean

call-seq:

p.power?

Returns true if p is a perfect power, i.e., if there exist integers a and b, with b > 1, such that p equals a raised to the power b.

Under this definition both 0 and 1 are considered to be perfect powers. Negative values of integers are accepted, but of course can only be odd perfect powers.

Returns:

  • (Boolean)

#powmod(b, c) ⇒ Object

Returns a raised to b modulo c.

Negative b is supported if an inverse a^-1 mod c exists. If an inverse doesn’t exist then a divide by zero is raised.



1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
# File 'ext/gmpz.c', line 1385

VALUE r_gmpz_powm(VALUE self, VALUE exp, VALUE mod)
{
  MP_INT *self_val, *res_val, *mod_val, *exp_val;
  VALUE res;
  int free_mod_val = 0;

  if (GMPZ_P(mod)) {
    mpz_get_struct(mod, mod_val);
    if (mpz_sgn(mod_val) <= 0) {
      rb_raise(rb_eRangeError, "modulus must be positive");
    }
  } else if (FIXNUM_P(mod)) {
    if (FIX2NUM(mod) <= 0) {
      rb_raise(rb_eRangeError, "modulus must be positive");
    }
    mpz_temp_alloc(mod_val);
    mpz_init_set_ui(mod_val, FIX2NUM(mod));
    free_mod_val = 1;
  } else if (BIGNUM_P(mod)) {
    mpz_temp_from_bignum(mod_val, mod);
    if (mpz_sgn(mod_val) <= 0) {
      mpz_temp_free(mod_val);
      rb_raise(rb_eRangeError, "modulus must be positive");
    }
    free_mod_val = 1;
  } else {
    typeerror_as(ZXB, "modulus");
  }
  mpz_make_struct_init(res, res_val);
  mpz_get_struct(self, self_val);

  if (GMPZ_P(exp)) {
    mpz_get_struct(exp, exp_val);
    if (mpz_sgn(mod_val) < 0) {
      rb_raise(rb_eRangeError, "exponent must be nonnegative");
    }
    mpz_powm(res_val, self_val, exp_val, mod_val);
  } else if (FIXNUM_P(exp)) {
    if (FIX2NUM(exp) < 0)
    {
      if (free_mod_val)
        mpz_temp_free(mod_val);
      rb_raise(rb_eRangeError, "exponent must be nonnegative");
    }
    mpz_powm_ui(res_val, self_val, FIX2NUM(exp), mod_val);
  } else if (BIGNUM_P(exp)) {
    mpz_temp_from_bignum(exp_val, exp);
    mpz_powm(res_val, self_val, exp_val, mod_val);
    mpz_temp_free(exp_val);
  } else {
    if (free_mod_val)
      mpz_temp_free(mod_val);
    typeerror_as(ZXB, "exponent");
  }
  if (free_mod_val)
    mpz_temp_free(mod_val);
  return res;
}

#probab_prime?Boolean

Number Theoretic Functions

Returns:

  • (Boolean)

#remove(f) ⇒ Object

Remove all occurrences of the factor f from n, returning the result as r. t, how many such occurrences were removed, is also returned.



1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
# File 'ext/gmpz.c', line 1820

VALUE r_gmpz_remove(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val, *res_val;
  VALUE res;
#if __GNU_MP_VERSION>2
  unsigned long removed_val;
#else
  int   removed_val;
#endif
  int free_arg_val = 0;

  mpz_get_struct(self, self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg,arg_val);
    if (mpz_sgn(arg_val) != 1)
      rb_raise(rb_eRangeError, "argument must be positive");
  } else if (FIXNUM_P(arg)) {
    if (FIX2NUM(arg) <= 0)
      rb_raise(rb_eRangeError, "argument must be positive");
    mpz_temp_alloc(arg_val);
    mpz_init_set_ui(arg_val, FIX2NUM(arg));
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    if (mpz_sgn(arg_val) != 1) {
      mpz_temp_free(arg_val);
      rb_raise(rb_eRangeError, "argument must be positive");
    }
  } else {
    typeerror(ZXB);
  }

  mpz_make_struct_init(res, res_val);
  removed_val = mpz_remove(res_val, self_val, arg_val);

  if (free_arg_val)
    mpz_temp_free(arg_val);

  return rb_assoc_new(res, INT2FIX(removed_val));
}

#rootObject

call-seq:

a.root(b)

Returns the truncated integer part of the bth root of a.

#scan0(starting_bit) ⇒ Object

Scan a, starting from bit starting_bit, towards more significant bits, until the first 0 bit is found. Return the index of the found bit.

If the bit at starting_bit is already what’s sought, then starting_bit is returned.

If there’s no bit found, then INT2FIX(ULONG_MAX) is returned. This will happen in scan0 past the end of a negative number.



2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
# File 'ext/gmpz.c', line 2219

VALUE r_gmpz_scan0(VALUE self, VALUE bitnr)
{
  MP_INT *self_val;
  int bitnr_val;
  mpz_get_struct (self, self_val);
  if (FIXNUM_P (bitnr)) {
    bitnr_val = FIX2INT (bitnr);
  } else {
    typeerror_as (X, "index");
  }
  return INT2FIX (mpz_scan0 (self_val, bitnr_val));
}

#scan1(starting_bit) ⇒ Object

Scan a, starting from bit starting_bit, towards more significant bits, until the first 1 bit is found. Return the index of the found bit.

If the bit at starting_bit is already what’s sought, then starting_bit is returned.

If there’s no bit found, then INT2FIX(ULONG_MAX) is returned. This will happen in scan1 past the end of a nonnegative number.



2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
# File 'ext/gmpz.c', line 2245

VALUE r_gmpz_scan1(VALUE self, VALUE bitnr)
{
  MP_INT *self_val;
  int bitnr_val;

  mpz_get_struct (self, self_val);

  if (FIXNUM_P (bitnr)) {
    bitnr_val = FIX2INT (bitnr);
  } else {
    typeerror_as (X, "index");
  }

  return INT2FIX (mpz_scan1 (self_val, bitnr_val));
}

#sgnObject

Returns +1 if a > 0, 0 if a == 0, and -1 if a < 0.



2081
2082
2083
2084
2085
2086
# File 'ext/gmpz.c', line 2081

VALUE r_gmpz_sgn(VALUE self)
{
  MP_INT *self_val;
  mpz_get_struct(self, self_val);
  return INT2FIX(mpz_sgn(self_val));
}

#sizeObject

Return the size of a measured in number of limbs. If a is zero, the returned value will be zero.

Since:

  • 0.4.19



2384
2385
2386
2387
2388
2389
# File 'ext/gmpz.c', line 2384

VALUE r_gmpz_size(VALUE self)
{
  MP_INT *self_val;
  mpz_get_struct(self, self_val);
  return INT2FIX(mpz_size(self_val));
}

#size_in_binObject

Return the size of a measured in number of digits in binary. The sign of a is ignored, just the absolute value is used. If a is zero the return value is 1.

Since:

  • 0.2.11



2363
2364
2365
2366
2367
2368
# File 'ext/gmpz.c', line 2363

VALUE r_gmpz_size_in_bin(VALUE self)
{
  MP_INT *self_val;
  mpz_get_struct (self, self_val);
  return INT2FIX (mpz_sizeinbase (self_val, 2));
}

#sizeinbaseObject Also known as: size_in_base

#sqrtObject

call-seq:

a.sqrt

Returns the truncated integer part of the square root of a.

#sqrt!Object

call-seq:

a.sqrt!

Sets a to the truncated integer part of its square root.

#sqrtremObject

#square?Boolean

call-seq:

p.square?

Returns true if p is a perfect square, i.e., if the square root of p is an integer. Under this definition both 0 and 1 are considered to be perfect squares.

Returns:

  • (Boolean)

#sub!(b) ⇒ Object

Subtracts b from a in-place, setting a to the difference. b must be an instance of one of:

  • GMP::Z

  • Fixnum

  • GMP::Q

  • GMP::F

  • Bignum



851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
# File 'ext/gmpz.c', line 851

VALUE r_gmpz_sub_self(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val;

  mpz_get_struct(self,self_val);

  if (GMPZ_P(arg)) {
    mpz_get_struct(arg, arg_val);
    mpz_sub (self_val, self_val, arg_val);
  } else if (FIXNUM_P(arg)) {
    if (FIX2NUM(arg) > 0)
      mpz_sub_ui (self_val, self_val, FIX2NUM(arg));
    else
      mpz_add_ui (self_val, self_val, -FIX2NUM(arg));
  } else if (BIGNUM_P(arg)) {
    mpz_temp_from_bignum(arg_val, arg);
    mpz_sub (self_val, self_val, arg_val);
    mpz_temp_free (arg_val);
  } else {
    typeerror (ZXB);
  }
  return Qnil;
}

#submul!(b, c) ⇒ Object

Sets a to a minus b times c. b and c must each be an instance of one of

  • GMP::Z

  • Fixnum

  • Bignum

Since:

  • 0.5.23



983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
# File 'ext/gmpz.c', line 983

static VALUE r_gmpz_submul_self(VALUE self, VALUE b, VALUE c)
{
  MP_INT *self_val, *b_val, *c_val;
  int free_b_val = 0;

  if (GMPZ_P(b)) {
    mpz_get_struct(b, b_val);
  } else if (FIXNUM_P(b)) {
    mpz_temp_alloc(b_val);
    mpz_init_set_si(b_val, FIX2NUM(b));
    free_b_val = 1;
  } else if (BIGNUM_P(b)) {
    mpz_temp_from_bignum(b_val, b);
    free_b_val = 1;
  } else {
    typeerror_as(ZXB, "addend");
  }
  mpz_get_struct(self, self_val);

  if (GMPZ_P(c)) {
    mpz_get_struct(c, c_val);
    mpz_submul(self_val, b_val, c_val);
  } else if (FIXNUM_P(c)) {
    if (FIX2NUM(c) < 0)
    {
      if (free_b_val) { mpz_temp_free(b_val); }
      rb_raise(rb_eRangeError, "multiplicand (Fixnum) must be nonnegative");
    }
    mpz_submul_ui(self_val, b_val, FIX2NUM(c));
  } else if (BIGNUM_P(c)) {
    mpz_temp_from_bignum(c_val, c);
    mpz_submul(self_val, b_val, c_val);
    mpz_temp_free(c_val);
  } else {
    if (free_b_val)
      mpz_temp_free(b_val);
    typeerror_as(ZXB, "multiplicand");
  }
  if (free_b_val)
    mpz_temp_free(b_val);
  return self;
}

#swap(b) ⇒ Object

Efficiently swaps the contents of a with b. b must be an instance of GMP::Z.

Returns:

  • nil



559
560
561
562
563
564
565
566
567
568
569
# File 'ext/gmpz.c', line 559

VALUE r_gmpz_swap(VALUE self, VALUE arg)
{
  MP_INT *self_val, *arg_val;
  if (!GMPZ_P(arg)) {
    rb_raise(rb_eTypeError, "Can't swap GMP::Z with object of other class");
  }
  mpz_get_struct(self, self_val);
  mpz_get_struct(arg, arg_val);
  mpz_swap(self_val,arg_val);
  return Qnil;
}

#tdivObject

call-seq:

n.tdiv(d)

Divides n by d, forming a quotient q. tdiv rounds q towards zero. The t stands for “truncate”.

q will satisfy n=q*d+r, and r will satisfy 0 <= abs( r ) < abs( d ).

This function calculates only the quotient.

#tmodObject

call-seq:

n.tmod(d)

Divides n by d, forming a remainder r. r will have the same sign as n. The t stands for “truncate”.

r will satisfy n=q*d+r, and r will satisfy 0 <= abs( r ) < abs( d ).

This function calculates only the remainder.

The remainder can be negative, so the return value is the absolute value of the remainder.

#to_dObject

TODO:

Implement mpz_fits_slong_p

Returns a as a Float if a fits in a Float.

Otherwise returns the least significant part of a, with the same sign as a.

If a is too big to fit in a Float, the returned result is probably not very useful. To find out if the value will fit, use the function mpz_fits_slong_p (Unimplemented).



619
620
621
622
623
624
625
# File 'ext/gmpz.c', line 619

VALUE r_gmpz_to_d(VALUE self)
{
  MP_INT *self_val;
  mpz_get_struct(self, self_val);

  return rb_float_new(mpz_get_d(self_val));
}

#to_iObject

TODO:

Implement mpz_fits_slong_p

Returns a as an Fixnum if a fits into a Fixnum.

Otherwise returns the least significant part of a, with the same sign as a.

If a is too big to fit in a Fixnum, the returned result is probably not very useful. To find out if the value will fit, use the function mpz_fits_slong_p (Unimplemented).



590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'ext/gmpz.c', line 590

VALUE r_gmpz_to_i(VALUE self)
{
  MP_INT *self_val;
  char *str;
  VALUE res;

  mpz_get_struct(self, self_val);
  if (mpz_fits_slong_p(self_val))
    return rb_int2inum(mpz_get_si(self_val));
  str = mpz_get_str(NULL, 0, self_val);
  res = rb_cstr2inum(str, 10);
  free(str);
  return res;
}

#to_s(*args) ⇒ Object

call-seq:

a.to_s(base = 10)
a.to_s(:bin)
a.to_s(:oct)
a.to_s(:dec)
a.to_s(:hex)

Returns a, as a String. If base is not provided, then the decimal representation will be returned.

From the GMP Manual:

Convert a to a string of digits in base base. The base argument may vary from 2 to 62 or from -2 to -36.

For base in the range 2..36, digits and lower-case letters are used; for -2..-36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used.



649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
# File 'ext/gmpz.c', line 649

VALUE r_gmpz_to_s(int argc, VALUE *argv, VALUE self)
{
  MP_INT *self_val;
  char *str;
  VALUE res;
  VALUE base;
  int base_val = 10;
  ID base_id;
  const char * bin_base = "bin";                            /* binary */
  const char * oct_base = "oct";                             /* octal */
  const char * dec_base = "dec";                           /* decimal */
  const char * hex_base = "hex";                       /* hexadecimal */
  ID bin_base_id = rb_intern(bin_base);
  ID oct_base_id = rb_intern(oct_base);
  ID dec_base_id = rb_intern(dec_base);
  ID hex_base_id = rb_intern(hex_base);  
  
  rb_scan_args(argc, argv, "01", &base);
  if (NIL_P(base)) { base = INT2FIX(10); }           /* default value */
  if (FIXNUM_P(base)) {
    base_val = FIX2INT(base);
    if ((base_val >=   2 && base_val <= 62) ||
        (base_val >= -36 && base_val <= -2)) {
      /* good base */
    } else {
      base_val = 10;
      rb_raise(rb_eRangeError, "base must be within [2, 62] or [-36, -2].");
    }
  } else if (SYMBOL_P(base)) {
    base_id = rb_to_id(base);
    if (base_id == bin_base_id) {
      base_val =  2;
    } else if (base_id == oct_base_id) {
      base_val =  8;
    } else if (base_id == dec_base_id) {
      base_val = 10;
    } else if (base_id == hex_base_id) {
      base_val = 16;
    } else {
      base_val = 10;  /* should raise an exception here. */
    }
  }

  Data_Get_Struct(self, MP_INT, self_val);
  str = mpz_get_str(NULL, base_val, self_val);
  res = rb_str_new2(str);
  free (str);

  return res;
}

#tshrObject

call-seq:

n.tshr(d)

Divides n by 2^d, forming a quotient q. tshr rounds q towards zero. The t stands for “truncate”.

q will satisfy n=q*d+r, and r will satisfy 0 <= abs( r ) < abs( d ).

This function calculates only the quotient.

#|Object

call-seq:

a | b

Returns a bitwise inclusive-or b. b must be an instance of one of the following:

  • GMP::Z

  • Fixnum

  • Bignum