プロジェクト

全般

プロフィール

Problem 9 » 履歴 » リビジョン 4

リビジョン 3 (Noppi, 2023/12/27 13:30) → リビジョン 4/6 (Noppi, 2023/12/28 03:01)

[ホーム](https://redmine.noppi.jp) - [[Wiki|Project Euler]] 
 # [[Problem 9]] 

 ## Special Pythagorean Triplet 
 A Pythagorean triplet is a set of three natural numbers, $a \lt b \lt c$, for which, 
 $$a^2 + b^2 = c^2.$$ 
 For example, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$. 
 There exists exactly one Pythagorean triplet for which $a + b + c = 1000$. 
 Find the product $abc$. 

 ## 特別なピタゴラス数 
 ピタゴラス数(ピタゴラスの定理を満たす自然数)とは a < b < c で以下の式を満たす数の組である. 
 $$a^2 + b^2 = c^2.$$ 
 例えば, $3^2 + 4^2 = 9 + 16 = 25 = 5^2$ である. 
 a + b + c = 1000 となるピタゴラスの三つ組が一つだけ存在する. 
 これらの積 abc を計算しなさい. 

 ```scheme 
 #!r6rs 
 #!chezscheme 

 (import (chezscheme)) 

 (define sum-1000-triplets answer-9 
   (let loop1 ([a 1] [result '()]) 1]) 
     (if (< 333 a) 
       result (error "answer-9" "answer-9 was not found!") 
       (let loop2 ([b a] [result result]) a]) 
         (if (< 499 b) 
           (loop1 (add1 a) result) a)) 
           (let ([c (- 1000 a b)]) 
             (loop2 (add1 b) (cons `(,a ,b ,c) result)))))))) 

 (define (pythagorean? a b c) 
   (if (= (+ (expt a 2) 
                       (expt b 2)) 
      
                    (expt c 2))) 

 (define pythagorean-triplets 
   (filter 
     (lambda (lis) 
       (apply pythagorean? lis)) 
     sum-1000-triplets)) 

 (assert (= (length pythagorean-triplets) 1)) 

 (define answer-9 
   (apply * (car pythagorean-triplets))) 2)) 
               (* a b c) 
               (loop2 (add1 b))))))))) 

 (printf "9: ~D~%" answer-9) 
 ```