Special pythagorean triplet: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added XPL0 example.)
m (a < b < c)
Line 100: Line 100:
=={{header|Julia}}==
=={{header|Julia}}==
<pre>
<pre>
julia> [(a, b, c) for a in 1:1000, b in 1:1000, c in 1:1000 if a + b + c == 1000 && a^2 + b^2 == c^2]
julia> [(a, b, c) for a in 1:1000, b in 1:1000, c in 1:1000 if a < b < c && a + b + c == 1000 && a^2 + b^2 == c^2]
2-element Vector{Tuple{Int64, Int64, Int64}}:
1-element Vector{Tuple{Int64, Int64, Int64}}:
(375, 200, 425)
(200, 375, 425)
(200, 375, 425)
</pre>
</pre>

Revision as of 17:19, 30 August 2021

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


Related task



ALGOL 68

Translation of: Wren

...but doesn't stop on the first solution (thus verifying there is only one). <lang algol68># find the Pythagorian triplet a, b, c where a + b + c = 1000 # FOR a TO 1000 DO

   INT a2 = a * a;
   FOR b FROM a + 1
   WHILE INT a plus b = a + b;
         a plus b < 1000
   DO
       INT c = 1000 - a plus b;
       IF c > b THEN
           IF a2 + b*b = c*c THEN
               print( ( "a = ", whole( a, 0 ), ", b = ", whole( b, 0 ), ", c = ", whole( c, 0 ), newline ) );
               print( ( "a + b + c = ", whole( a plus b + c, 0 ), newline ) );
               print( ( "a * b * c = ", whole( a * b * c, 0 ), newline ) )
           FI
       FI
   OD

OD</lang>

Output:
a = 200, b = 375, c = 425
a + b + c = 1000
a * b * c = 31875000

ALGOL W

Translation of: Wren

...but doesn't stop on the first solution (thus verifying there is only one). <lang algolw>% find the Pythagorian triplet a, b, c where a + b + c = 1000 % for a := 1 until 1000 do begin

   integer a2, b;
   a2 := a * a;
   b  := a + 1;
   while a + b < 1000 do begin
       integer c;
       c := 1000 - ( a + b );
       if c > b then begin
           if a2 + b*b = c*c then begin
               write( i_w := 1, s_w := 0, "a = ", a, ", b = ", b, ", c = ", c );
               write( i_w := 1, s_w := 0, "a + b + c = ", a + b + c );
               write( i_w := 1, s_w := 0, "a * b * c = ", a * b * c )
           end if_a2_plus_b2_e_c2
       end if_b_gt_c ;
       b := b + 1
   end while_a_plus_b_lt_1000

end for_a .</lang>

Output:
a = 200, b = 375, c = 425
a + b + c = 1000
a * b * c = 31875000

Go

Translation of: Wren

<lang go>package main

import (

   "fmt"
   "time"

)

func main() {

   start := time.Now()
   for a := 3; ; a++ {
       for b := a + 1; ; b++ {
           c := 1000 - a - b
           if c <= b {
               break
           }
           if a*a+b*b == c*c {
               fmt.Printf("a = %d, b = %d, c = %d\n", a, b, c)
               fmt.Println("a + b + c =", a+b+c)
               fmt.Println("a * b * c =", a*b*c)
               fmt.Println("\nTook", time.Since(start))
               return
           }
       }
   }

}</lang>

Output:
a = 200, b = 375, c = 425
a + b + c = 1000
a * b * c = 31875000

Took 77.664µs

Julia

julia> [(a, b, c) for a in 1:1000, b in 1:1000, c in 1:1000 if a < b < c && a + b + c == 1000 && a^2 + b^2 == c^2]
1-element Vector{Tuple{Int64, Int64, Int64}}:
 (200, 375, 425)

Python

Python 3.8.8 (default, Apr 13 2021, 15:08:03)
Type "help", "copyright", "credits" or "license" for more information.
>>> [(a, b, c) for a in range(1, 1000) for b in range(a, 1000) for c in range(1000) if a + b + c == 1000 and a*a + b*b == c*c]
[(200, 375, 425)]


Raku

<lang perl6>hyper for 1..998 -> $a {

   my $a2 = $a²;
   for $a + 1 .. 999 -> $b {
       my $c = 1000 - $a - $b;
       last if $c < $b;
       say "$a² + $b² = $c²\n$a  + $b  + $c = 1000" and exit if $a2 + $b² == $c²
   }

}</lang>

Output:
200² + 375² = 425²
200  + 375  + 425 = 1000

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


Incidentally, even though we are told there is only one solution, it is almost as quick to verify this by observing that, since a < b < c, the maximum value of a must be such that 3a + 2 = 1000 or max(a) = 332. The following version ran in 0.015 seconds and, of course, produced the same output: <lang ecmascript>for (a in 3..332) {

   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)")
       }
       b = b + 1
   }

}</lang>

XPL0

<lang XPL0>int N, M, A, B, C; for N:= 1 to sqrt(1000) do

   for M:= N+1 to sqrt(1000) do
       [A:= M*M - N*N; \Euclid's formula
        B:= 2*M*N;
        C:= M*M + N*N;
        if A+B+C = 1000 then
           IntOut(0, A*B*C);
       ]</lang>
Output:
31875000