Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created June 18, 2009 16:07
Show Gist options
  • Save Koitaro/131975 to your computer and use it in GitHub Desktop.
Save Koitaro/131975 to your computer and use it in GitHub Desktop.
namespace path {::tcl::mathop ::tcl::mathfunc}
proc primes n {
set half [/ [- $n 1] 2]
set xs [lrepeat $half 1]
set p 0; set root [/ [sqrt $n] 2]
while {$p < $root} {
if {[lindex $xs $p]} {
set step [+ [* 2 $p] 3]
set index [+ $p $step]
while {$index < $half} { lset xs $index 0; incr index $step }
}
incr p
}
set answer 2
foreach i [lsearch -all $xs 1] { lappend answer [+ [* 2 $i] 3] }
set answer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment