How to Supersize Math in Ruby with Huge Numbers

By Christopher Haupt

Unlike a pocket calculator, or even the calculator on a smartphone, Ruby has amazing support for some truly gigantic numbers. Give the following a try:

2.2.2 :022 > 1234567890 * 9876543210 * 12345678998765432234567890
=> 150534112319147377922666710346041538891241000

There are 45 digits in that number! You can use the exponent operator (**) to raise a number by a certain power:

2.2.2 :026 > 10**2
=> 100

Try coming up with some really big numbers of your own and do some arithmetic on them.

If you haven’t learned about exponents yet, all you need to know is that it’s the same as taking a number and multiplying it by itself the number of times indicated by the exponent number. So 10**2 means multiply 10 by itself two times: 10 * 10. Sometimes you hear someone talking about exponents using the phrase some number raised to a certain power. In this example, 10 is raised to the second power.