The sieve of eratosthenes
TCL
#!/usr/bin/tclsh
#Sieve of Eratosthenes in TCL
# by Ted Mierzwinski
proc printList nums {
set val 0
foreach i $nums {
if {$i == 1} {puts -nonewline "$val "}
incr val
}
puts "\b"
}
proc removeMultiples {nums mult max} {
for {set i [expr $mult*$mult]} {$i <= $max} {set i [expr $i + $mult]} {
lset nums [expr $i] 0
}
return $nums
}
proc nextPrime {nums prev max} {
for {set i [expr $prev + 1]} {$i <= $max} {incr i} {
if {[lindex $nums $i] == 1} { return $i }
}
return -1
}
if {$argc == 0} {
return
}
set max [lindex $argv 0]
set nums {0 0}
for {set i 2} {$i <= $max} {incr i} {
lappend nums 1
}
set p 2
while {$p != -1} {
set nums [removeMultiples $nums $p $max]
set p [nextPrime $nums $p $max]
}
printList $nums
stats
It is
Friday December 05, 2008 10:40 am
This page served 875 times
This page last modified: April 14, 2008 11:28 am
Your IP address is: 38.103.63.61
You are browsing using: CCBot/1.0 (+http://www.commoncrawl.org/bot.html)
You are browsing from: United States.
badcomputer.org's uptime: 10:40:55 up 24 days, 14:51, 2 users, load average: 0.00, 0.00, 0.00
local
home | unix stuff | dir2ogg | sneetchalizer | wmainfo | q&d guide to permissions | q&d guide to tar and gzip | code | MS rant | browser shootout | linux & iAudio X5 | photos | music | programming poetry | sieve of Eratosthenes | plea | rain | suffer | archive | about | recipes | compaqr3000 | sitemap
search
credits
This page, and all pages on this site were created and are maintained by Darren Kirby using valid XHTML 1.0 and CSS, and are ©copyright 2002 - 2008. The Penguin image was created by Tukka, and is used by permission. Inspiration for the look of this site was provided by Eric A. Meyer's CSS gallery. This website runs on Gentoo Linux. It is served by Apache. PHP and MySQL hold together the backend.