Class: LibTom::Math::Bignum

Inherits:
Numeric
  • Object
show all
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

Constructor Details

#initializeObject

Class Method Details

.random_of_sizeObject

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

#absObject

#add_modulusObject

#coerceObject

#divmodObject

#eql?Boolean

Returns:

  • (Boolean)

#even?Boolean

Returns:

  • (Boolean)

#exponent_modulusObject

#extended_euclidianObject

#greatest_common_divisorObject Also known as: gcd

#hashObject

#initialize_copyObject

#inverse_modulusObject

#is_divisible_by_some_primes?Boolean

Returns:

  • (Boolean)

#is_prime?Boolean

Returns:

  • (Boolean)

#is_square?Boolean

Returns:

  • (Boolean)

#jacobiObject

#least_common_multipleObject Also known as: lcm

#left_shift_digitsObject Also known as: multiply_by_x_pow_n

#mod_2nObject Also known as: mod_pow2, modulo_2n, modulo_pow2

#multiply_modulusObject

#next_primeObject

#nonzero?Boolean

Returns:

  • (Boolean)

#nth_rootObject

#num_bitsObject

#num_miller_rabin_trialsObject



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

#odd?Boolean

Returns:

  • (Boolean)

#passes_fermat_primality?Boolean

Returns:

  • (Boolean)

#passes_miller_rabin?Boolean

Returns:

  • (Boolean)

#remainderObject

#right_shift_digitsObject Also known as: divide_by_x_pow_n

#sizeObject

#square_modulusObject

#square_rootObject

#squaredObject

#subtract_modulusObject

#to_fObject

#to_sObject

#zero!Object

#zero?Boolean

Returns:

  • (Boolean)

#|Object

#~Object