Pythagorean triples: Difference between revisions

Content added Content deleted
(Larger D example)
(→‎Tcl: Added implementation)
Line 880: Line 880:
1000000 (70229, 808950)
1000000 (70229, 808950)
10000000 (702309, 9706567)</lang>
10000000 (702309, 9706567)</lang>

=={{header|Tcl}}==
Using the efficient method based off the Wikipedia article:
{{works with|Tcl|8.6}}
<lang tcl>package require Tcl 8.6
proc gentrips {limit {a 3} {b 4} {c 5}} {
if {$a + $b + $c <= $limit} {
yield [list 1 1 $a $b $c]
for {set i 2} {$i*$a+$i*$b+$i*$c <= $limit} {incr i} {
yield [list 1 0 [expr {$a*$i}] [expr {$b*$i}] [expr {$c*$i}]]
}
gentrips $limit [expr {$a + 2*($c-$b)}] [expr {2*($a+$c) - $b}] [expr {2*($a-$b) + 3*$c}]
gentrips $limit [expr {$a + 2*($b+$c)}] [expr {2*($a+$c) + $b}] [expr {2*($a+$b) + 3*$c}]
gentrips $limit [expr {2*($b+$c) - $a}] [expr {2*($c-$a) + $b}] [expr {2*($b-$a) + 3*$c}]
}
return 0
}
proc countPythagoreanTriples n {
coroutine p eval "yield;gentrips $n"
while {[lassign [p] more p a b c;set more]} {
incr count
incr primitive $p
}
list $count $primitive
}
lassign [countPythagoreanTriples 100] count primitive
puts "$count triples, $primitive primitive"</lang>