primes/primes.tcl

30 lines
504 B
Tcl
Raw Normal View History

2011-03-08 07:01:27 +00:00
#!/usr/bin/env tclsh
# TCL version of a Prime Number Finder
# haplo@mindstab.net
if { $argc == 0 } {
puts "Useage: primes.tcl \[Max Num\]"
exit
}
set MAX [lindex $argv 0]
if { $MAX < 1 } {
puts "Max Num must be greater than 1"
exit
}
for { set cnt 3} {$cnt <= $MAX} {incr cnt 2} {
set max_test [expr sqrt($cnt)]
set isPrime 1
for {set test 3} {$test <= $max_test} {incr test 2} {
if { [expr $cnt % $test] == 0 } {
set isPrime 0;
break
}
}
if { $isPrime == 1 } {
puts $cnt
}
}