Skip to content

Commit dd85750

Browse files
committed
Merge bowling change
1 parent 781c814 commit dd85750

File tree

6 files changed

+91
-0
lines changed

6 files changed

+91
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@
750750
"practices": [],
751751
"prerequisites": [],
752752
"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
753761
}
754762
]
755763
},

exercises/practice/pythagorean-triplets/.docs/instructions.md

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#lang racket
2+
3+
(provide triplets-with-sum)
4+
5+
(define (triplets-with-sum p)
6+
(error "Not implemented yet"))

0 commit comments

Comments
 (0)