[GS] (20121104) A Pythagorean triple is a triple of positive integers (a,b,c) such that a right triangle exists with legs a,b and hypotenuse c satisfying : a**2 + b**2 = c**2 For instance the most obvious solution is (3,4,5). A scatter plot of the legs (a,b) and their symetries show parabolic patterns. [http://wfr.tcl.tk/fichiers/images/pytriplet.jpg] ====== # pytriplet.tcl # Author: Gerard Sookahet # Date: 04 Nov 2012 # Description: Plot pythagorean triplet package require Tk bind all {exit} proc Pytriplet {H N} { .c delete all set pix [image create photo] .c create image 0 0 -anchor nw -image $pix set HN [expr {double($H)/(2*$N)}] set mid [expr {$H/2}] for {set a 1} {$a <= $N} {incr a} { for {set b $a} {$b <= $N} {incr b} { set c [expr {sqrt($a*$a + $b*$b)}] if {$c == round($c)} then { if [Prime $a $b] { set ahn [expr {$a*$HN}] set bhn [expr {$b*$HN}] set mpahn [expr {int($mid + $ahn)}] set mpbhn [expr {int($mid + $bhn)}] set x(1) $mpahn set y(1) $mpbhn set x(2) [expr {int($mid - $ahn)}] set y(2) [expr {int($mid - $bhn)}] set x(3) $mpahn set y(3) [expr {int($mid - $bhn)}] set x(4) [expr {int($mid - $ahn)}] set y(4) $mpbhn foreach i {1 2 3 4} {$pix put #00FF00 -to $x($i) $y($i)} foreach i {1 2 3 4} {$pix put #00FF00 -to $y($i) $x($i)} update idletask } } } } } proc Prime {a b} { for {set i 2} {$i <= $a} {incr i} { set ai [expr {double($a)/$i}] set bi [expr {double($b)/$i}] if {$ai == round($ai) && $bi == round($bi)} {return 0} } return 1 } proc Main {H N} { wm title . "Pythagorean" pack [canvas .c -width $H -height $H -bg black] set f1 [frame .f1 -relief sunken -borderwidth 2] pack $f1 -fill x button $f1.bu -text Run -width 12 -bg blue -fg white \ -command "Pytriplet $H $N" button $f1.bq -text Quit -width 5 -bg blue -fg white -command exit eval pack [winfo children $f1] -side left } Main 600 6000 ====== For more mathematical details, see : Manuel Benito and Juan L. Varona, '' Pythagorean triangles with legs less than n'', Journal of Computational and Applied Mathematics 143 (2002) pp117–126 [http://www.unirioja.es/cu/jvarona/downloads/Benito-Varona-JCAM-Publicado.pdf] <> Mathematics | Graphics