nvec ← ##.sieve nvec                    ⍝ Sieve of Eratosthenes
                                        ⍝ Eratosthenes of Cyrene (¯276-¯194)

Removes multiples of numbers within the argument vector. Thus sieve 2..⍵ returns
those  primes in the range 2..⍵.  An illustration of most of the D-function con-
structs: left argument defaulting; local definition; guards; tail recursion.

(muse:

    Amongst  other  accomplishments, Eratosthenes estimated the circumference of
    the  Earth to a surprising accuracy. This is even more impressive if you re-
    member  that,  as  late  as  Columbus's time, many people still believed the
    Earth to be flat.

    http://en.wikipedia.org/wiki/Eratosthenes  reports Eratasthenes' estimate to
    be 16% too large, taking the value of the stadion to be 185m. However, if we
    take  a  value  of  157.2, which some people derive from Pliny, the estimate
    comes in at only 3% too small.
)

Example:

    sieve 2 to 100
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

See also: to

Back to: contents

Back to: Dyalog APL

Trouble seeing APL font?