Re: Code needed for Mathematica re Pythagorean triples
- From: "Wlodzimierz Holsztynski (Wlod)" <sennajawa@xxxxxxxxx>
- Date: Sun, 06 May 2007 05:36:33 EDT
I can see that this is a rare case when a bit of mathematical knowledge may hurt your algorithm (am I right?). Mine is ignorant of the known parametrization of the P. triples. It's in perl but almost like in C.
%%%%%%%%% start code %%%%%%%%%
#!/usr/bin/perl
#
# author: wh
# Pythagorean triples with the given area or perimeter
use integer;
print "Enter n ==> ";
chomp($n = <>);
print "\n\nPythagorean triangles with perimeter = $n\n\n";
$start = times();
$per=0;
# for($a=1; 2*$a*$a < ($n-2*$a)*($n-2*$a); ++$a) perfect but prone to overflow
for($a=3; 17*$a < 5*$n; ++$a)
{ for($b=12*($n-$a)/31; ; ++$b)
{ $c = $n-$a-$b;
last if ($ab=$a*$a + $b*$b) > ($cc=$c*$c);
if($ab == $cc)
{ print "$a $b $c\n";
++$per;
last
} } }
$endT = times();
print "\nThere are $per Pyth. triangles with\nperimeter = $n\n\n";
print " The computation took *** ",$endT-$start," ***\n\n";
print "********************\n\n";
print "\n\nPythagorean triangles with area = $n / 2\n\n";
$start = times();
$ar=0;
for($a=3; ($b=$n/$a) > $a; ++$a)
{ next if ($a*$b != $n);
$C = $a*$a + $b*$b;
for($c=$b+($a*$a)/(2*$b); ; $c=($c+$C/$c)/2)
{ last if $c*$c < $C;
if($c*$c==$C)
{ print "$a $b $c\n";
++$ar;
last
} } }
$endT = times();
print "\n\nThere are $ar Pyth. triangles with area = $n / 2\n\n\n";
print " The computation took *** ", $endT-$start, " ***\n\n";
%%%%%% end code %%%%%%%
Enjoy,
Wlod
.
- Follow-Ups:
- Prev by Date: Re: A bit scared but I dare to...
- Next by Date: sum (for k=1 to +inf) {N*(T^k)*[(1/(1+d))^k;0]
- Previous by thread: Re: A bit scared but I dare to...
- Next by thread: Re: Code needed for Mathematica re Pythagorean triples
- Index(es):