Project Euler

Project Euler Problem 9

steloflute 2012. 5. 28. 00:46

Problem 9

25 January 2002

A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.


Answer:
31875000

 

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Euler9 {
    class Program {
        static void Main(string[] args) {
            for (int a = 1; a < 1000; a++) {
                for (int b = a + 1; b < 1000; b++) {
                    int c = 1000 - a - b;
                    if (c < 1) continue;
                    if (a * a + b * b == c * c) {
                        Console.Write("{0} * {1} * {2} = {3}", a, b, c, a * b * c);
                    }
                }
            }
        }
    }
}
 
Perl
sub problem9 {
 for ( my $a = 1 ; $a < 1000 ; $a++ ) {
  for ( my $b = $a + 1 ; $b < 1000 ; $b++ ) {
   my $c = 1000 - $a - $b;
   if ( $c < 1 ) { next; }
   if ( $a * $a + $b * $b == $c * $c ) {
    print "$a * $b * $c = " . $a * $b * $c;
   }
  }
 }
}
 
Python
def problem9():
    for a in xrange(1, 1000):
        for b in xrange(a + 1, 1000):
            c = 1000 - a - b;
            if c < 1: continue
            if a * a + b * b == c * c:
                print a, "*", b, "*", c, "=", a * b * c
 

 

 

Go

 

func problem9() {
 for a := 1; a < 1000; a++ {
  for b := a + 1; b < 1000; b++ {
   c := 1000 - a - b
   if c < 1 {
    continue
   }
   if a*a+b*b == c*c {
    fmt.Println(a, b, c, a*b*c)
   }
  }
 }

}

func main() {
 problem9()
}



C++


void problem9() {

for (int a = 1; a < 1000; a++) {

for (int b = a + 1; b < 1000; b++) {

int c = 1000 - a - b;

if (c < 1) continue;

if (a * a + b * b == c * c) {

printf("%d * %d * %d = %d\n", a, b, c, a * b * c);

}

}

}

}

 

 

Bash

 

function problem9 {
    local a
    local b
    local c
    for a in {1..999}; do
        for b in $(seq $a 999); do
            ((c=1000-a-b))
            if ((c<1)); then continue; fi
            if ((a*a + b*b == c*c)); then
                echo $a $b $c $((a*b*c))
            fi
        done
    done
}

problem9



Javascript


function problem9() {

 for (var a = 1; a < 1000; a++) {

  for (var b = a + 1; b < 1000; b++) {

   var c = 1000 - a - b;

   if (c < 1) continue;

   if (a * a + b * b == c * c) {

    return a * b * c;

   }

  }

 }

}


problem9()


Racket

 

#lang racket

(define (problem9)

  (for* ([a (in-range 1 1000)]

         [b (in-range (+ 1 a) 1000)])

    (define c (- 1000 a b))

    (when (and (> c b) 

               (= (+ (sqr a) (sqr b))

                  (sqr c)))

      (display (list a b c (* a b c))))))

(problem9)

 

'Project Euler' 카테고리의 다른 글

Project Euler Problem 11  (0) 2012.05.29
Project Euler Problem 10  (0) 2012.05.28
Project Euler Problem 8  (0) 2012.05.28
Project Euler Problem 7  (0) 2012.05.28
Project Euler Problem 6  (0) 2012.05.28