Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pythagorean-triplet exercise #373

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,17 @@
"strings"
]
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
"uuid": "f33d976d-5189-4fcb-b0ac-42879a1067bf",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"topics": [
"math"
]
},
{
"slug": "rail-fence-cipher",
"name": "Rail Fence Cipher",
Expand Down
23 changes: 23 additions & 0 deletions exercises/practice/pythagorean-triplet/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Instructions

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}`.
19 changes: 19 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"pythagorean-triplet.el"
],
"test": [
"pythagorean-triplet-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the triplet.",
"source": "Problem 9 at Project Euler",
"source_url": "https://projecteuler.net/problem=9"
}
30 changes: 30 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;;; pythagorean-triplet.el --- Pythagorean Triplet (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

; For every Pythagorean triplet with total a + b + c = n,
; a² + b² = c²
; <=> a² + b² = (n - a - b)², substituting c
; <=> 0 = n² - 2*n*a - 2*n*b + 2*a*b
; <=> (2*n - 2*a) b = (n² - 2*n*a)
; <=> b = (n² - 2*n*a) / (2*n - 2*a)

;;; Code:

(require 'cl-lib)

(defun triplets-with-sum (n)
(cl-labels
((recur (start)
(cl-loop for a from start
for numerator = (* n (- n a a))
for denominator = (* 2 (- n a))
for b = (/ numerator denominator)
always (< a b)
until (= 0 (% numerator denominator))
finally return (cons (list a b (- n a b)) (recur (1+ a))))))
(unless (< n 2)
(recur 1))))

(provide 'pythagorean-triplet)
;;; pythagorean-triplet.el ends here
31 changes: 31 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
53 changes: 53 additions & 0 deletions exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
;;; pythagorean-triplet-test.el --- Tests for Pythagorean Triplet (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(load-file "pythagorean-triplet.el")
(declare-function triplets-with-sum "pythagorean-triplet.el" (n))


(ert-deftest triplets-whose-sum-is-12 ()
(should (equal '((3 4 5)) (triplets-with-sum 12))))


(ert-deftest triplets-whose-sum-is-108 ()
(should (equal '((27 36 45)) (triplets-with-sum 108))))


(ert-deftest triplets-whose-sum-is-1000 ()
(should (equal '((200 375 425)) (triplets-with-sum 1000))))


(ert-deftest no-matching-triplets-for-1001 ()
(should (equal '() (triplets-with-sum 1001))))


(ert-deftest returns-all-matching-triplets ()
(should (equal '((9 40 41) (15 36 39)) (triplets-with-sum 90))))


(ert-deftest several-matching-triplets ()
(should (equal '((40 399 401)
(56 390 394)
(105 360 375)
(120 350 370)
(140 336 364)
(168 315 357)
(210 280 350)
(240 252 348))
(triplets-with-sum 840))))


(ert-deftest triplets-for-large-number ()
(should (equal '((1200 14375 14425)
(1875 14000 14125)
(5000 12000 13000)
(6000 11250 12750)
(7500 10000 12500))
(triplets-with-sum 30000))))


(provide 'pythagorean-triplet-test)
;;; pythagorean-triplet-test.el ends here
13 changes: 13 additions & 0 deletions exercises/practice/pythagorean-triplet/pythagorean-triplet.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;;; pythagorean-triplet.el --- Pythagorean Triplet (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun triplets-with-sum (n)
(error "Delete this S-Expression and write your own implementation"))


(provide 'pythagorean-triplet)
;;; pythagorean-triplet.el ends here