Calculating the largest known prime in Ruby - JP Camara

Ask questions Research chat →

https://jpcamara.com/2024/11/26/looking-to-impress.html · scraped

ruby

Attachments

Scraped Content

— 271 words · 2026-05-19 12:33:06 UTC ·

Excerpt

Looking to impress your Ruby friends by calculating the largest known prime, 2 ** 136_279_841-1? On Ruby 3.4.0-preview2 and earlier, 2 ** 136_279_841-1 logs a warning and returns Infinity 😔: Thanks to @mametter, Ruby 3.4 will handle this calculation just fine! See Do not round a**b to infinity. Knowing this, you excitedly use your ruby manager of choice to pull down ruby master: ```plain text rvm install ruby-head ``` You run ruby -e "puts 2 ** 136_279_841-1", and your excitement is slowly eroded. An hour into calculating, you terminate the command in frustration 😫. Is @mametter a liar?! As it turns out, there is critically important library you need for accelerating “Bignum” calculations: GMP, the GNU Multiple Precision Arithmetic Library. It’s even specifically mentioned in the CRuby guide to building ruby. Without it, you can kiss your largest prime calculating dreams goodbye 👋. You reinstall ruby head, making sure gmp is available ```plain text brew install gmp rvm reins
Looking to impress your Ruby friends by calculating the largest known prime, 2 ** 136_279_841-1? On Ruby 3.4.0-preview2 and earlier, 2 ** 136_279_841-1 logs a warning and returns Infinity 😔: Thanks to @mametter, Ruby 3.4 will handle this calculation just fine! See Do not round a**b to infinity. Knowing this, you excitedly use your ruby manager of choice to pull down ruby master: ```plain text rvm install ruby-head ``` You run ruby -e "puts 2 ** 136_279_841-1", and your excitement is slowly eroded. An hour into calculating, you terminate the command in frustration 😫. Is @mametter a liar?! As it turns out, there is critically important library you need for accelerating “Bignum” calculations: GMP, the GNU Multiple Precision Arithmetic Library. It’s even specifically mentioned in the CRuby guide to building ruby. Without it, you can kiss your largest prime calculating dreams goodbye 👋. You reinstall ruby head, making sure gmp is available ```plain text brew install gmp rvm reinstall ruby-head --with-gmp-dir=$(brew --prefix gmp) ``` With a bit of hope in your heart, you try again: ```plain text ruby -e "puts 2 ** 136_279_841-1" ``` Success! @mametter was telling the truth! Within around 5 seconds, your terminal is filled with a beautiful output of 41,024,320 digits. Your Ruby friends cheer and carry you off on their shoulders. This was all inspired by Matz’s keynote at RubyConf 2024 - where he mentioned that Ruby 3.4 can now calculate the largest known prime. For fun, I tried it on my mac and just let it keep running - 2 hours later, it was still running! I’d never heard of GMP, but now I know!

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation