File tree 6 files changed +91
-0
lines changed
exercises/practice/pythagorean-triplets
6 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 750
750
"practices" : [],
751
751
"prerequisites" : [],
752
752
"difficulty" : 6
753
+ },
754
+ {
755
+ "slug" : " pythagorean-triplets" ,
756
+ "name" : " pythagorean-triplets" ,
757
+ "uuid" : " 630a11f5-e640-4e4d-af01-30f3bbcf495c" ,
758
+ "practices" : [],
759
+ "prerequisites" : [],
760
+ "difficulty" : 4
753
761
}
754
762
]
755
763
},
Original file line number Diff line number Diff line change
1
+ {
2
+ "authors" : [" blakelewis" ],
3
+ "files" : {
4
+ "solution" : [
5
+ " pythagorean-triplets.rkt"
6
+ ],
7
+ "test" : [
8
+ " pythagorean-triplets-test.rkt"
9
+ ],
10
+ "example" : [
11
+ " .meta/example.rkt"
12
+ ]
13
+ },
14
+ "blurb" : " "
15
+ }
Original file line number Diff line number Diff line change
1
+ #lang racket
2
+
3
+ (provide triplets-with-sum)
4
+
5
+ (define (primitives pmax)
6
+ (for*/list
7
+ ([m (in-naturals 2 )]
8
+ #:break (> (* 2 (* m (add1 m))) pmax)
9
+ [n (in-range (add1 (remainder m 2 )) m 2 )]
10
+ #:when (= 1 (gcd m n)))
11
+ (define a (- (* m m) (* n n)))
12
+ (define b (* 2 (* m n)))
13
+ (define c (+ (* m m) (* n n)))
14
+ (sort (list a b c) < )))
15
+
16
+ (define (triplets-with-sum p)
17
+ (define (scale-up triple)
18
+ (define perimeter (for/sum ([t triple]) t))
19
+ (cond
20
+ [(zero? (remainder p perimeter))
21
+ (define k (quotient p perimeter))
22
+ (for/list ([t triple]) (* k t))]
23
+ [else #f ]))
24
+ (sort (filter-map scale-up (primitives p)) < #:key car))
Original file line number Diff line number Diff line change
1
+ #lang racket/base
2
+
3
+ (require "pythagorean-triplets.rkt " )
4
+
5
+ (module+ test
6
+
7
+ (require rackunit rackunit/text-ui)
8
+
9
+ (define suite
10
+ (test-suite
11
+ "Return Pythagorean triplets with given perimeter "
12
+
13
+ (test-equal? "triplets whose sum is 12 "
14
+ (triplets-with-sum 12 ) '((3 4 5 )))
15
+
16
+ (test-equal? "triplets whose sum is 108 "
17
+ (triplets-with-sum 108 ) '((27 36 45 )))
18
+
19
+ (test-equal? "triplets whose sum is 1000 "
20
+ (triplets-with-sum 1000 ) '((200 375 425 )))
21
+
22
+ (test-equal? "no matching triplets for 1001 "
23
+ (triplets-with-sum 1001 ) '() )
24
+
25
+ (test-equal? "returns all matching triplets "
26
+ (triplets-with-sum 90 ) '((9 40 41 ) (15 36 39 )))
27
+
28
+ (test-equal? "several matching triplets "
29
+ (triplets-with-sum 840 )
30
+ '((40 399 401 ) (56 390 394 ) (105 360 375 ) (120 350 370 )
31
+ (140 336 364 ) (168 315 357 ) (210 280 350 ) (240 252 348 )))
32
+
33
+ (test-equal? "triplets for large number "
34
+ (triplets-with-sum 30000 )
35
+ '((1200 14375 14425 ) (1875 14000 14125 ) (5000 12000 13000 )
36
+ (6000 11250 12750 ) (7500 10000 12500 )))))
37
+
38
+ (run-tests suite))
Original file line number Diff line number Diff line change
1
+ #lang racket
2
+
3
+ (provide triplets-with-sum)
4
+
5
+ (define (triplets-with-sum p)
6
+ (error "Not implemented yet " ))
You can’t perform that action at this time.
0 commit comments