Special pythagorean triplet
Special pythagorean triplet is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
- The following problem is taken from Project Euler
Ring
<lang ring> load "stdlib.ring" see "working..." + nl
time1 = clock() for a = 1 to 1000
for b = a+1 to 1000 for c = b+1 to 1000 if (pow(a,2)+pow(b,2)=pow(c,2)) if a+b+c = 1000 see "a = " + a + " b = " + b + " c = " + c + nl exit 3 ok ok next next
next
time2 = clock() time3 = time2/1000 - time1/1000 see "Elapsed time = " + time3 + " s" + nl see "done..." + nl </lang>
- Output:
working... a = 200 b = 375 c = 425 Elapsed time = 497.61 s done...
Wren
Very simple approach, only takes 0.013 seconds even in Wren. <lang ecmascript>var a = 3 while (true) {
var b = a + 1 while (true) { var c = 1000 - a - b if (c <= b) break if (a*a + b*b == c*c) { System.print("a = %(a), b = %(b), c = %(c)") System.print("a + b + c = %(a + b + c)") System.print("a * b * c = %(a * b * c)") return } b = b + 1 } a = a + 1
}</lang>
- Output:
a = 200, b = 375, c = 425 a + b + c = 1000 a * b * c = 31875000