{"id":54,"date":"2022-11-01T12:00:00","date_gmt":"2022-11-01T12:00:00","guid":{"rendered":"https:\/\/blog.marcinpolkowski.com\/?p=54"},"modified":"2022-12-03T01:38:30","modified_gmt":"2022-12-03T01:38:30","slug":"how-to-calculate-the-value-of-the-biggest-prime-we-know","status":"publish","type":"post","link":"https:\/\/blog.marcinpolkowski.com\/?p=54","title":{"rendered":"How to calculate the value of the biggest prime we know?"},"content":{"rendered":"<p>I hope that I don&#8217;t need to introduce prime numbers to anyone. They are fascinating for all mathematic enthusiasts. We know a lot of prime numbers, but we have no formula to calculate them. Search for the biggest primes is an ongoing competition. People are looking for the biggest primes among the so-called Mersenne Primes. Mersenne Primes are primes that can be expressed as 2<sup>n<\/sup>-1. The record is currently held by 2<sup>82,589,933<\/sup>\u22121 with 24,862,048 digits, found by GIMPS in December 2018 (more on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Largest_known_prime_number\">Wikipedia<\/a>).<\/p>\n<p>We can find a value of 2<sup>82,589,933<\/sup>-1 online (for example beginning and the end of the number on Wikipedia linked above). But what if we would like to calculate the value ourselves? We have multiple options. For the sake of this article, I selected 2:<\/p>\n<h3>bc &#8211; an arbitrary precision calculator language<\/h3>\n<p><b>bc<\/b> is a linux\/unix command line calculator that supports arbitrary precision:<\/p>\n<p><center><a href=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/11\/bc_example.png\"><img decoding=\"async\" src=\"https:\/\/blog.marcinpolkowski.com\/wp-content\/uploads\/2022\/11\/bc_example.png\" style=\"width:60%\"><\/a><\/center><\/p>\n<p>We can run the following in the terminal to calculate the value of 2<sup>82,589,933<\/sup>-1. We use tr and sed to pack the result in one line.<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">echo \"Testing BC:\"\ntime echo \"2^82589933-1\" \\\n    | bc \\\n    | tr \"\\n\" \" \" \\\n    | sed 's\/\\\\ \/\/g' \\\n    &gt; bc.txt\n<\/code><\/pre>\n<p><\/center><br \/>\nSince the calculation is not trivial the answer is not coming instantaneously. On my 2021 MacBook pro with M1 Max processor it took 30 minutes to get the result: &gt;24 megabytes of digits.<\/p>\n<h3>Python<\/h3>\n<p>Yes, Python! Python supports arbitrary precision integers by default. There is a slight complication that was added recently for security reasons: we need to increase the default length of the number that can be converted to a string. Let&#8217;s consider the following code:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">import sys\nsys.set_int_max_str_digits(int(3e7))\n\nnumber = 82589933\n\nprint(2**number-1, end=' ')\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>And run it in the terminal:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">echo \"Testing Python\":\ntime python python.py &gt; python.txt\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>It takes python almost 3 hours to compute the result.<\/p>\n<h2>Results<\/h2>\n<p>Finally, we can confirm that both results are the same:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">echo \"Comparing Results\":\nshasum bc.txt\nshasum python.txt\n<\/code><\/pre>\n<p><\/center><\/p>\n<p>Which should give result as following:<br \/>\n<center><\/p>\n<pre style=\"width:90%; overflow:auto; text-align:left;\"><code class=\"language-bash\">Comparing Results:\n9abebcdaf11efd28f1797d6b2744cbcb0cc66e52  bc.txt\n9abebcdaf11efd28f1797d6b2744cbcb0cc66e52  python.txt\n<\/code><\/pre>\n<p><\/center><\/p>\n<h3>Summary<\/h3>\n<p>If you ever need to calculate integers bigger then 64 or 128 bits, now you know how.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope that I don&#8217;t need to introduce prime numbers to anyone. They are fascinating for all mathematic enthusiasts. We know a lot of prime numbers, but we have no formula to calculate them. Search for the biggest primes is an ongoing competition. People are looking for the biggest primes among the so-called Mersenne Primes. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6],"tags":[],"class_list":["post-54","post","type-post","status-publish","format-standard","hentry","category-linux","category-python"],"_links":{"self":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":8,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions\/63"}],"wp:attachment":[{"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.marcinpolkowski.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}