REBOL [
    Title:   "Sieve of Erathostenes"
    Author:  "Aldo Calpini"
    Date:    03-Jul-2001
    File:    %sieve.r
]

NUM: to-integer to-string system/script/args
NUM: either NUM < 1 [ 1 ] [ NUM ]

while [ NUM > 0 ] [
    count: 0
    comment [
        flags: array/initial 1 8192
    ]

    flags: copy []
    for i 0 8192 1 [
        insert tail flags 1
    ]
    flags: head flags

    for i 2 8192 1 [
        p: pick flags i
        if p = 1 [
            k: i + i            
            while [ k <= 8192 ] [
                change at flags k 0
                k: k + i
            ]
            count: count + 1
        ]
    ]
    NUM: NUM - 1
]

write %output.rebol rejoin [ "Count: " count ]