スキップしてメイン コンテンツに移動

Project Euler - Problem 9

問題

  • 原文

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

  • 日本語訳

    a + b + c = 1000となるピタゴラスの三つ組が一つだけ存在する. このa,b,cの積を計算しなさい.

解答

何の変哲もない2重ループです。

(define (square n) (* n n))
(define (solve)
  (let outer-loop ((i 1))
    (let inner-loop ((j 1))
      (define k (sqrt (+ (square i) (square j))))
      (cond
       ((= (+ i j k) 1000) (* i j (inexact->exact k)))
       ((> (+ i j k) 1000) (outer-loop (+ i 1)))
       (else               (inner-loop (+ j 1)))))))
(define (main argv)
  (display (solve))
  (newline))

コメント