[Shootout-list] Tcl submissions

Robert Seeger Robert Seeger <rhseeger@gmail.com>
Mon, 21 Mar 2005 08:14:20 -0500


Actually, Simon Geard's submission (a greatly eenhanced version of my
updated submission) is much faster than mine. Since it works on 8.4,
it can be includeded now. I'm pasting the code below.

Rob Seeger


On Mon, 21 Mar 2005 00:06:37 -0800, Brent Fulgham <bfulg@pacbell.net> wrote:
> 
> On Mar 18, 2005, at 5:10 AM, Robert Seeger wrote:
> > Also, I noticed that nsieve is listed as "no program". Should I
> > resubmit my program for this test, or is it just taking a while to
> > process?
> 
> I think this solution required Tcl 8.5, which I do not have available
> via Debian
> (I won't build from source as a general policy).
> 
> Once Tcl 8.5 is available, I'll add the test.
> 
> Thanks,
> 
> -Brent

package require Tcl 8.4

proc main {n} {
    foreach value [list $n [incr n -1] [incr n -1]] {
        set num [expr { int(pow(2, $value) * 10000) }]
        puts [format "Primes up to %8d %8d" $num [nsieve $num]]
    }
}

proc nsieve {n} {
    set data {}
    for {set i 0} {$i <= $n} {incr i} {
        lappend data 1
    }

    set count 0
    for {set i 2} {$i <= $n} {incr i} {
        if { [lindex $data $i] } {
            for {set j [expr {$i + $i}]} {$j <= $n} {incr j $i} {
                lset data $j 0
            }
            incr count
        }
    }
    
    return $count
}

main [lindex $argv 0]