November 30th, 2024

Calculating the largest known prime in Ruby

JP Camara explains how to calculate the largest known prime number using Ruby 3.4 with GMP support, achieving a result of 41,024,320 digits after overcoming initial errors.

Read original articleLink Icon
Calculating the largest known prime in Ruby

JP Camara discusses the process of calculating the largest known prime number, specifically 2 ** 136,279,841 - 1, using Ruby. In earlier versions of Ruby, such as 3.4.0-preview2, attempting this calculation resulted in a warning and an output of Infinity. However, with the upcoming Ruby 3.4, the calculation can be performed correctly, provided that the GNU Multiple Precision Arithmetic Library (GMP) is installed. The author details the steps to install Ruby with GMP support, which is crucial for handling large number calculations. After successfully setting up the environment, the calculation completes in about five seconds, producing an output of 41,024,320 digits. This achievement was inspired by a keynote at RubyConf 2024, where it was highlighted that Ruby 3.4 could handle such calculations. The author humorously reflects on the initial frustration and the eventual success, celebrating the newfound capability with fellow Ruby enthusiasts.

- Ruby 3.4 can calculate the largest known prime number with the right setup.

- The GNU Multiple Precision Arithmetic Library (GMP) is essential for large number calculations in Ruby.

- The calculation of 2 ** 136,279,841 - 1 produces an output of 41,024,320 digits.

- Initial attempts without GMP resulted in errors and frustration.

- The process was inspired by a keynote at RubyConf 2024.

Link Icon 4 comments
By @ubutler - 5 months
Nice. You can do the same in Python like so and surprisingly it only takes a couple seconds to a minute to get the print out, no need for importing any special libraries (just `sys` to enable printing large numbers).

```python

import sys sys.set_int_max_str_digits(0) # Allows for printing very large numbers.

x = (2 * 136_279_841) - 1

print(x)

```

By @lerax - 5 months
I've been told that Ruby was the slowest language of the universe, much more than Python. That's interesting challenge!
By @faebi - 5 months
Sounds like a new possible attack vector?