diff --git a/config.json b/config.json index ca8eaa6..b384c59 100644 --- a/config.json +++ b/config.json @@ -411,6 +411,23 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "pythagorean-triplet", + "name": "Pythagorean Triplet", + "uuid": "23992d00-d1e4-4ee3-9ca7-50885124d813", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, + { + "slug": "pythagorean-triplets", + "name": "Pythagorean Triplets", + "uuid": "630a11f5-e640-4e4d-af01-30f3bbcf495c", + "practices": [], + "prerequisites": [], + "difficulty": 4, + "status": "deprecated" + }, { "slug": "affine-cipher", "name": "Affine Cipher", @@ -716,6 +733,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "bowling", + "name": "Bowling", + "uuid": "f6ca2cb9-f169-4c3e-8a64-6d3326b01d70", + "practices": [], + "prerequisites": [], + "difficulty": 6 + }, { "slug": "bank-account", "name": "Bank Account", @@ -767,22 +792,6 @@ ], "prerequisites": [], "difficulty": 8 - }, - { - "slug": "bowling", - "name": "Bowling", - "uuid": "f6ca2cb9-f169-4c3e-8a64-6d3326b01d70", - "practices": [], - "prerequisites": [], - "difficulty": 6 - }, - { - "slug": "pythagorean-triplets", - "name": "Pythagorean Triplet", - "uuid": "630a11f5-e640-4e4d-af01-30f3bbcf495c", - "practices": [], - "prerequisites": [], - "difficulty": 4 } ] }, diff --git a/exercises/practice/pythagorean-triplet/.docs/instructions.md b/exercises/practice/pythagorean-triplet/.docs/instructions.md new file mode 100644 index 0000000..ced833d --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.docs/instructions.md @@ -0,0 +1,23 @@ +# Description + +A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which, + +```text +a² + b² = c² +``` + +and such that, + +```text +a < b < c +``` + +For example, + +```text +3² + 4² = 5². +``` + +Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`. + +For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`: `{200, 375, 425}`. diff --git a/exercises/practice/pythagorean-triplet/.docs/introduction.md b/exercises/practice/pythagorean-triplet/.docs/introduction.md new file mode 100644 index 0000000..3453c6e --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.docs/introduction.md @@ -0,0 +1,19 @@ +# Introduction + +You are an accomplished problem-solver, known for your ability to tackle the most challenging mathematical puzzles. +One evening, you receive an urgent letter from an inventor called the Triangle Tinkerer, who is working on a groundbreaking new project. +The letter reads: + +> Dear Mathematician, +> +> I need your help. +> I am designing a device that relies on the unique properties of Pythagorean triplets — sets of three integers that satisfy the equation a² + b² = c². +> This device will revolutionize navigation, but for it to work, I must program it with every possible triplet where the sum of a, b, and c equals a specific number, N. +> Calculating these triplets by hand would take me years, but I hear you are more than up to the task. +> +> Time is of the essence. +> The future of my invention — and perhaps even the future of mathematical innovation — rests on your ability to solve this problem. + +Motivated by the importance of the task, you set out to find all Pythagorean triplets that satisfy the condition. +Your work could have far-reaching implications, unlocking new possibilities in science and engineering. +Can you rise to the challenge and make history? diff --git a/exercises/practice/pythagorean-triplet/.meta/config.json b/exercises/practice/pythagorean-triplet/.meta/config.json new file mode 100644 index 0000000..4689d68 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "blakelewis" + ], + "contributors": [ + "SimaDovakin", + "keiravillekode" + ], + "files": { + "solution": [ + "pythagorean-triplet.rkt" + ], + "test": [ + "pythagorean-triplet-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "blurb": "Given an integer N, find all Pythagorean triplets for which a + b + c = N.", + "source": "A variation of Problem 9 from Project Euler", + "source_url": "https://projecteuler.net/problem=9" +} diff --git a/exercises/practice/pythagorean-triplet/.meta/example.rkt b/exercises/practice/pythagorean-triplet/.meta/example.rkt new file mode 100644 index 0000000..0f4c43c --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/example.rkt @@ -0,0 +1,24 @@ +#lang racket + +(provide triplets-with-sum) + +(define (primitives pmax) + (for*/list + ([m (in-naturals 2)] + #:break (> (* 2 (* m (add1 m))) pmax) + [n (in-range (add1 (remainder m 2)) m 2)] + #:when (= 1 (gcd m n))) + (define a (- (* m m) (* n n))) + (define b (* 2 (* m n))) + (define c (+ (* m m) (* n n))) + (sort (list a b c) < ))) + +(define (triplets-with-sum p) + (define (scale-up triple) + (define perimeter (for/sum ([t triple]) t)) + (cond + [(zero? (remainder p perimeter)) + (define k (quotient p perimeter)) + (for/list ([t triple]) (* k t))] + [else #f])) + (sort (filter-map scale-up (primitives p)) < #:key car)) diff --git a/exercises/practice/pythagorean-triplet/.meta/tests.toml b/exercises/practice/pythagorean-triplet/.meta/tests.toml new file mode 100644 index 0000000..719620a --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/tests.toml @@ -0,0 +1,31 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[a19de65d-35b8-4480-b1af-371d9541e706] +description = "triplets whose sum is 12" + +[48b21332-0a3d-43b2-9a52-90b2a6e5c9f5] +description = "triplets whose sum is 108" + +[dffc1266-418e-4daa-81af-54c3e95c3bb5] +description = "triplets whose sum is 1000" + +[5f86a2d4-6383-4cce-93a5-e4489e79b186] +description = "no matching triplets for 1001" + +[bf17ba80-1596-409a-bb13-343bdb3b2904] +description = "returns all matching triplets" + +[9d8fb5d5-6c6f-42df-9f95-d3165963ac57] +description = "several matching triplets" + +[f5be5734-8aa0-4bd1-99a2-02adcc4402b4] +description = "triplets for large number" diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.rkt b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.rkt new file mode 100644 index 0000000..0f31c51 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.rkt @@ -0,0 +1,38 @@ +#lang racket/base + +(require "pythagorean-triplet.rkt") + +(module+ test + + (require rackunit rackunit/text-ui) + + (define suite + (test-suite + "Return Pythagorean triplets with given perimeter" + + (test-equal? "triplets whose sum is 12" + (triplets-with-sum 12) '((3 4 5))) + + (test-equal? "triplets whose sum is 108" + (triplets-with-sum 108) '((27 36 45))) + + (test-equal? "triplets whose sum is 1000" + (triplets-with-sum 1000) '((200 375 425))) + + (test-equal? "no matching triplets for 1001" + (triplets-with-sum 1001) '()) + + (test-equal? "returns all matching triplets" + (triplets-with-sum 90) '((9 40 41) (15 36 39))) + + (test-equal? "several matching triplets" + (triplets-with-sum 840) + '((40 399 401) (56 390 394) (105 360 375) (120 350 370) + (140 336 364) (168 315 357) (210 280 350) (240 252 348))) + + (test-equal? "triplets for large number" + (triplets-with-sum 30000) + '((1200 14375 14425) (1875 14000 14125) (5000 12000 13000) + (6000 11250 12750) (7500 10000 12500))))) + + (run-tests suite)) diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet.rkt b/exercises/practice/pythagorean-triplet/pythagorean-triplet.rkt new file mode 100644 index 0000000..9234a91 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet.rkt @@ -0,0 +1,6 @@ +#lang racket + +(provide triplets-with-sum) + +(define (triplets-with-sum p) + (error "Please implement 'triplets-with-sum'"))