Rebol3 Code Examplex


Piprimes

Generate primes that satisfy a special digit pattern.

Rebol [
    title: "Rosetta code: Piprimes"
    file:  %Piprimes.r3
    url:   https://rosettacode.org/wiki/Piprimes
]

pi-primes: function [
    "Returns the sequence of running prime counts up to (but not including) the nth prime"
    limit [integer!] "number of primes at which to stop"
][
    curr: running: 0                  ;; current number being tested, count of primes found
    out: copy []                      ;; accumulates running prime-count at each step

    while [true] [
        ++ curr                       ;; advance to next candidate
        if prime? curr [ ++ running ] ;; bump count if candidate is prime
        if running = limit [ break ]  ;; stop when we've hit the target prime count
        append out running            ;; record current count before next iteration
    ]
    out                               ;; return the sequence
]

counts: pi-primes 22
forall counts [
    prin pad counts/1 -3
    if zero? mod index? counts 10 [print ""]
]
print rejoin [LF "Pi-Primes 0-21: " as-green length? counts]

Output:

  0  1  2  2  3  3  4  4  4  4
  5  5  6  6  6  6  7  7  8  8
  8  8  9  9  9  9  9  9 10 10
 11 11 11 11 11 11 12 12 12 12
 13 13 14 14 14 14 15 15 15 15
 15 15 16 16 16 16 16 16 17 17
 18 18 18 18 18 18 19 19 19 19
 20 20 21 21 21 21 21 21
Pi-Primes 0-21: 78