Special pythagorean triplet: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: updated output, added wording to the REXX section header.)
(→‎{{header|Ring}}: illustrating straightforward method vs. faster method.)
Line 821: Line 821:
=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<lang ring>
? "working..."
load "stdlib.ring"
see "working..." + nl


? "brute force method:"
time1 = clock()
time1 = clock()
for a = 1 to 1000
for a = 1 to 998
for b = a+1 to 1000
aa = a * a
for c = b+1 to 1000
for b = a + 1 to 999
if (pow(a,2)+pow(b,2)=pow(c,2))
bb = aa + b * b
if a+b+c = 1000
c = 1000 - a - b
see "a = " + a + " b = " + b + " c = " + c + nl
if bb = c * c
exit 3
? "a = " + a + " b = " + b + " c = " + c
ok
exit 2
ok
ok
next
next
next
next
next
time2 = clock()


? "Elapsed time = " + (time2 - time1) / 1000 + " ms"

? "quick method:"

time1 = clock()
n = 1000
n2 = n >> 1
for a = 1 to n2
b = n * (n2 - a)
if b % (n - a) = 0 exit ok
next
b /= (n - a)
? "a = " + a + " b = " + b + " c = " + (n - a - b)
time2 = clock()
time2 = clock()

time3 = time2/1000 - time1/1000
see "Elapsed time = " + time3 + " s" + nl
? "Elapsed time = " + (time2 - time1) / 1000 + " ms"
see "done..." + nl
see "done..."</lang>
</lang>
{{out}}
{{out}}
Quicker method is about 1000x faster.
<pre>
working...
<pre>working...
brute force method:
a = 200 b = 375 c = 425
Elapsed time = 927.21 ms
quick method:
a = 200 b = 375 c = 425
a = 200 b = 375 c = 425
Elapsed time = 497.61 s
Elapsed time = 0.90 ms
done...
done...</pre>
</pre>


=={{header|Wren}}==
=={{header|Wren}}==