Version 5 of Primes

Updated 2002-12-17 13:34:30

Primes are positive integers that can only be divided (without remainder) by 1 and themselves. Examples from RS's collection:

 1234567891 987654321987654329

Here's a very simple prime checker, which returns 1 for primes, else the first found divisor (and the quotient):

 proc primetest n {
    set max [expr wide(sqrt($n))]
    if {$n%2==0} {return [list 2 [expr $n/2]]}
    for {set i 3} {$i<=$max} {incr i 2} {
       if {$n%$i==0} {return [list $i [expr $n/$i]]}
    }
    return 1
 }

AM commented in the Tcl chatroom: "The trick: divide by the first ten or twenty primes, after that: numbers of the form 6n+1 and 6n+5 may act as divisors. If you use only numbers of the form I mentioned, you gain about 50% with no loss of information."


The first 6 million primes: http://www.geocities.com/ResearchTriangle/Thinktank/2434/prime/primenumbers.html

Mathworld: http://mathworld.wolfram.com/PrimeNumber.html


Category Concept | Arts and crafts of Tcl-Tk programming