August 18, 2017

primorial zoop!

Filed under: Uncategorized — Benjamin Vulpes @ 6:54 a.m.

Mircea Popescu asked for computations of the largest primorial to fit within first 515 bytes, and then 4096 later for reasonable reasons having to do with RSA padding and machine word widths. And so my adventures in remedial mathematics continue, with an implementation of the Sieve of Eratosthenes (which, no, I also had never heard about until Popescu mentioned it.

The sieve implementation is reasonable, although it struck me while writing this that the loop macro has quite clearly infected me to the point where Everything Must be Implemented in Terms of It. Also worth noting is the hilaribad hack of summing lists of primes sieved out from below 4096 and then re-sieving until a resulting list sums that is less than 2^4096.


(defun sieve (max)
  (let ((candidates (make-array (+ 1 max) :element-type 'bit :initial-element 1)))
    (setf (aref candidates 0) 0)
    (setf (aref candidates 1) 0)
    (loop for p from 2 below max
         (if (= 1 (aref candidates p))
                for multiple = (* p 2) then (+ multiple p)
                  (>= multiple (+ 1 max))
                  (setf (aref candidates multiple) 0))))
       for c across candidates
       for i = 0 then (+ 1 i)
       when (= c 1)
       collect i)))

(defun primorial (bits)
     for maxprime downfrom 4096
     and candidate = (apply #'* (sieve maxprime))
     when (< candidate (expt 2 bits))
     return candidate))

Runs handily in under a second on this computer:

Evaluation took:
  0.817 seconds of real time
  0.822183 seconds of total run time (0.800534 user, 0.021649 system)
  [ Run times consist of 0.053 seconds GC time, and 0.770 seconds non-GC time. ]
  100.61% CPU
  1,961,326,656 processor cycles
  191,607,056 bytes consed


Matching Diana Coman's and PeterL's. I recall mod6 confirming this number as well, but cannot find the log line. Do write in if you find it, listeners.

This excursion into the fascinating worlds of primes now complete, I'm going to return to my studies in remedial binary arithmetic (which are going fine, albeit slowly due to the annoying handicap of having my workstation not proximate to my sleep station; and a poverty of compute and space for compute away from the prying hands of toddlers in my current domicile; all of which are scheduled for remediation this fall, the orchestration of which is all very much under steam), which I'd left off in disgust recently having realized I was trying to write overgeneralized solutions the problem of reading a random n-byte integer from a byte stream.

There really is no better training ground for the intelligent, curious, and hardworking than The Republic of Bitcoin.


  1. The closest thing mod6 published was this (, which doesn't seem to match what you pumped out. Also, your result spills right off the page.

RSS feed for comments on this post. TrackBack URL


« The CIA's flickr account --- "Hey kid, want to buy some VERBOTEN HATE SPEECH?" »