
# pytriplet.tcl
# Author: Gerard Sookahet
# Date: 04 Nov 2012
# Description: Plot pythagorean triplet
package require Tk
bind all <Escape> {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 6000For 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 [1]arjen - 2012-11-06 07:25:02You know of course that there is an analytical (parametric) solution for them:
a = 2uv
b = u**2 - v**2
c = u**2 + v**2(u and v positive integers), u > v. For the well-known (a,b,c) = (3,4,5) solution, (u,v) = (2,1)).Having such a solution does not lessen the magic of these triples, though.
