Class: LibTom::Math::Bignum
- Inherits:
-
Numeric
- Object
- Numeric
- LibTom::Math::Bignum
- Defined in:
- lib/libtom/math/bignum.rb,
ext/libtom/lt_math.c
Overview
What is LibTom::Math::Bignum?
An almost drop in replacement for the built in ruby Bignum. Overall the LibTomMath library is faster, can handler larger numbers and has more features than the built in ruby Bignum.
LibTom::Math::Bignum provides full support for all arithmetic operations in the same manner as Ruby’s Bignum. In addition, it provides many number theory operations.
So what does it provide that Ruby’s built in Bignum doesn’t.
It provides number theory operations such as primality tests, random number generation, the extended euclidian algorithm and additional modulus operations.
Hmm, it must be missing something then.
Yes, LibTom::Math::Bignum does not behave in the exact same way that Ruby’s Bignum behaves. These are the only ways in which LibTom::Math::Bignum differes from Ruby’s Bignum
-
Does not implement [] to provide “bit vector” style access.
-
Does not implement ~ to provide bitwise negation.
-
Forces all Numeric instances to LibTom::Math::Bignum when doing operations. A Bignum + Float => bignum.
That’s all nice and good, but how about some examples?
These are just a few, explore the API.
Generate big random numbers
big = LibTom::Math::Bignum::random_of_size(100_000)
big.num_bits # => 1000000
big.to_s.size # => 30108 (digits)
Test if a big number is prime
big.is_prime?
Get the Greatest Common Divisor of 2 numbers
a = LibTom::Math::Prime::random_of_size(56) # => 42732001016016959
b = LibTom::Math::Prime::random_of_size(56) # => 63810767126601779
c = LibTom::Math::Prime::random_of_size(56) # => 58312287814792003
ab = a * b
bc = b * c
g = ab.greatest_common_divisor(bc) # => 63810767126601779
g == b # => true
Class Method Summary collapse
Instance Method Summary collapse
- #% ⇒ Object (also: #mod, #modulo)
- #& ⇒ Object
- #* ⇒ Object (also: #multiply)
- #** ⇒ Object
- #+ ⇒ Object (also: #add)
- #- ⇒ Object (also: #subtract)
- #-@ ⇒ Object
- #/ ⇒ Object (also: #divide, #quo, #div)
- #<< ⇒ Object
- #<=> ⇒ Object
- #== ⇒ Object
- #>> ⇒ Object
- #[] ⇒ Object
- #^ ⇒ Object
- #abs ⇒ Object
- #add_modulus ⇒ Object
- #coerce ⇒ Object
- #divmod ⇒ Object
- #eql? ⇒ Boolean
- #even? ⇒ Boolean
- #exponent_modulus ⇒ Object
- #extended_euclidian ⇒ Object
- #greatest_common_divisor ⇒ Object (also: #gcd)
- #hash ⇒ Object
- #initialize ⇒ Object constructor
- #initialize_copy ⇒ Object
- #inverse_modulus ⇒ Object
- #is_divisible_by_some_primes? ⇒ Boolean
- #is_prime? ⇒ Boolean
- #is_square? ⇒ Boolean
- #jacobi ⇒ Object
- #least_common_multiple ⇒ Object (also: #lcm)
- #left_shift_digits ⇒ Object (also: #multiply_by_x_pow_n)
- #mod_2n ⇒ Object (also: #mod_pow2, #modulo_2n, #modulo_pow2)
- #multiply_modulus ⇒ Object
- #next_prime ⇒ Object
- #nonzero? ⇒ Boolean
- #nth_root ⇒ Object
- #num_bits ⇒ Object
- #num_miller_rabin_trials ⇒ Object
- #odd? ⇒ Boolean
- #passes_fermat_primality? ⇒ Boolean
- #passes_miller_rabin? ⇒ Boolean
- #remainder ⇒ Object
- #right_shift_digits ⇒ Object (also: #divide_by_x_pow_n)
- #size ⇒ Object
- #square_modulus ⇒ Object
- #square_root ⇒ Object
- #squared ⇒ Object
- #subtract_modulus ⇒ Object
- #to_f ⇒ Object
- #to_s ⇒ Object
- #zero! ⇒ Object
- #zero? ⇒ Boolean
- #| ⇒ Object
- #~ ⇒ Object
Constructor Details
#initialize ⇒ Object
Class Method Details
.random_of_size ⇒ Object
Instance Method Details
#% ⇒ Object Also known as: mod, modulo
#& ⇒ Object
#* ⇒ Object Also known as: multiply
#** ⇒ Object
#+ ⇒ Object Also known as: add
#- ⇒ Object Also known as: subtract
#-@ ⇒ Object
#/ ⇒ Object Also known as: divide, quo, div
#<< ⇒ Object
#<=> ⇒ Object
#== ⇒ Object
#>> ⇒ Object
#[] ⇒ Object
#^ ⇒ Object
#abs ⇒ Object
#add_modulus ⇒ Object
#coerce ⇒ Object
#divmod ⇒ Object
#eql? ⇒ Boolean
#even? ⇒ Boolean
#exponent_modulus ⇒ Object
#extended_euclidian ⇒ Object
#greatest_common_divisor ⇒ Object Also known as: gcd
#hash ⇒ Object
#initialize_copy ⇒ Object
#inverse_modulus ⇒ Object
#is_divisible_by_some_primes? ⇒ Boolean
#is_prime? ⇒ Boolean
#is_square? ⇒ Boolean
#jacobi ⇒ Object
#least_common_multiple ⇒ Object Also known as: lcm
#left_shift_digits ⇒ Object Also known as: multiply_by_x_pow_n
#mod_2n ⇒ Object Also known as: mod_pow2, modulo_2n, modulo_pow2
#multiply_modulus ⇒ Object
#next_prime ⇒ Object
#nonzero? ⇒ Boolean
#nth_root ⇒ Object
#num_bits ⇒ Object
#num_miller_rabin_trials ⇒ Object
68 69 70 |
# File 'lib/libtom/math/bignum.rb', line 68 def num_miller_rabin_trials LibTom::Math::num_miller_rabin_trials(self.num_bits) end |