Law of cosines - triples: Difference between revisions

Content added Content deleted
Line 789: Line 789:
Extra credit:
Extra credit:
60° integer triples in the range 1..10000 where the sides are not all the same length: 18394</pre>
60° integer triples in the range 1..10000 where the sides are not all the same length: 18394</pre>

=={{header|Phix}}==
Using a simple flat sequence of 100 million elements (well within the language limits) proved significantly faster than a dictionary (5x or so).
<lang Phix>sequence squares = repeat(0,10000*10000)
for c=1 to 10000 do
squares[c*c] = c
end for

function solve(integer angle, maxlen, bool samelen=true)
sequence res = {}
for a=1 to maxlen do
integer a2 = a*a
for b=a to maxlen do
integer c2 = a2+b*b
if angle!=90 then
if angle=60 then c2 -= a*b
elsif angle=120 then c2 += a*b
else crash("angle must be 60/90/120")
end if
end if
integer c = iff(c2>length(squares)?0:squares[c2])
if c!=0 and c<=maxlen then
if samelen or a!=b or b!=c then
res = append(res,{a,b,c})
end if
end if
end for
end for
return res
end function

procedure show(string fmt,sequence res, bool full=true)
printf(1,fmt,{length(res),iff(full?sprint(res):"")})
end procedure

puts(1,"Integer triangular triples for sides 1..13:\n")
show("Angle 60 has %2d solutions: %s\n",solve( 60,13))
show("Angle 90 has %2d solutions: %s\n",solve( 90,13))
show("Angle 120 has %2d solutions: %s\n",solve(120,13))
show("Non-equilateral angle 60 triangles for sides 1..10000: %d%s\n",solve(60,10000,false),false)</lang>
{{out}}
<pre style="font-size: 11px">
Integer triangular triples for sides 1..13:
Angle 60 has 15 solutions: {{1,1,1},{2,2,2},{3,3,3},{3,8,7},{4,4,4},{5,5,5},{5,8,7},{6,6,6},{7,7,7},{8,8,8},{9,9,9},{10,10,10},{11,11,11},{12,12,12},{13,13,13}}
Angle 90 has 3 solutions: {{3,4,5},{5,12,13},{6,8,10}}
Angle 120 has 2 solutions: {{3,5,7},{7,8,13}}
Non-equilateral angle 60 triangles for sides 1..10000: 18394
</pre>


=={{header|Python}}==
=={{header|Python}}==