Last Modified: 2023-10-12 13:30:36Z
Table of Contents
You don’t need to think deeply when you have to deal with big numbers in Python. By default Python can handle them just like the other numbers. It is one of wonderful features of Python.
But in Ruby, dealing with big numbers is a big thing. Even though there is a bignum feature in Ruby.
Ruby’s OpenSSL gem provides big number handling library.
OpenSSL library is a default gem of Ruby, you don’t need to install separately.
1. OpenSSL::BN
BN should mean an abbreviation of Big Number.
require 'openssl'
a_bn = OpenSSL::BN.new()
b_bn = OpenSSL::BN.new()
You can do arithmetic operations.
a_bn + b_bn
a_bn - b_bn
a_bn * b_bn
1.1. Modular Inverse
Ruby’s default pow()
has limited features.
2. Reference
Official Ruby gem of OpenSSL https://github.com/ruby/openssl
Original Implementation of pow function https://github.com/ruby/ruby/blob/94788a6d13a136f28daca2a51ad7945c52b2311d/bignum.c#L7128