Version 3 of A little primes toy

Updated 2003-06-25 14:56:54

if 0 {Richard Suchenwirth 2003-06-24 - This educational Tcltoy (written on a PocketPC, but should run anywhere) shows the numbers from 1 to 100 on a canvas - see $about for more. Maybe not a good starter for Teaching programming to children, but a nice visualitation of the Sieve of Erastothenes, and a factorization helper...

http://mini.net/files/primes.jpg }

 set about "primes.tcl
  Richard Suchenwirth 2003
  Powered by Tcl/Tk!

  Click on a number to highlight its multiples, and see its prime factors on top.
  Click Clear to remove highlighting.
  Primes are never highlighted. They are multiples only of 1, which is no prime.
  (Hint: 2 3 5 7)"

 package require Tk ;# to make it work with tclkit...
 proc main {} {
    frame .f
    label .f.l -width 26 -bg white -textvar info
    button .f.a -text About  -command {tk_messageBox -message $about}
    button .f.c -text Clear -command {.c itemconfig txt -fill black}
    button .f.x -text X -command exit
    eval pack [winfo children .f] -side left -fill y
    pack .f [canvas .c]
    set x 20; set y 20
    set n 0
    foreach row {1 2 3 4 5 6 7 8 9 10} {
       foreach col {1 2 3 4 5 6 7 8 9 10} {
          .c create text $x $y -text [incr n] -tag txt
          incr x 20
       }
       set x 20; incr y 20
    }
    .c bind txt <1> {hilite .c}
    wm geometry . 240x268+0+0
 }
 proc hilite w {
    set id [$w find withtag current]
    set ::info $id=[join [primefactors $id] *]
    if {$id == 1} return
    for {set i [expr $id+1]} {$i<=100} {incr i} {
       if {$i%$id == 0} {
          $w itemconfig $i -fill orange
       }
    }
 }
 proc primefactors n {
    set res {}
    for {set i 2} {$i <= $n} {incr i} {
       while {$n%$i == 0} {
          set n [expr {$n/$i}]
          lappend res $i
       }
    }
    set res
 }
 main