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 #263

Merged
merged 1 commit into from
Jun 5, 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 @@ -493,6 +493,17 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
"uuid": "548fa68b-98ff-4c3f-87ae-ef3a0cc605c1",
"practices": [],
"prerequisites": [],
"difficulty": 6,
"topics": [
"math"
]
},
{
"slug": "all-your-base",
"name": "All Your Base",
Expand Down
6 changes: 6 additions & 0 deletions exercises/practice/pythagorean-triplet/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Hints

## General

- The `$t0-9` registers can be used to temporarily store values
- The instructions specify which registers are used as input and output
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Instructions append

## Registers

| Register | Usage | Type | Description |
| -------- | ------------ | ------- | ---------------------------- |
| `$a0` | input | integer | N, sum of sides of triangle |
| `$a1` | input/output | address | a values |
| `$a2` | input/output | address | b values |
| `$a3` | input/output | address | c values |
| `$v0` | output | integer | number of triplets |
| `$t0-9` | temporary | any | for temporary storage |
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": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"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"
}
59 changes: 59 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | -------------------------------------------- |
# | `$a0` | input | integer | N, sum of sides of triangle |
# | `$a1` | input/output | address | a values |
# | `$a2` | input/output | address | b values |
# | `$a3` | input/output | address | c values |
# | `$v0` | output | integer | number of triplets |
# | `$t0` | temporary | address | start of a values |
# | `$t1` | temporary | integer | a |
# | `$t2` | temporary | integer | b |
# | `$t3` | temporary | integer | c |
# | `$t5` | temporary | integer | remainder |
# | `$t6` | temporary | integer | numerator |
# | `$t7` | temporary | integer | denominator |

# 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)
#
# The denominator is never 0, as N exceeds a side length.

.globl triplets_with_sum

triplets_with_sum:
move $t0, $a1 # start of a values
blt $a0, 2, return # Stop immediately if N < 2
move $t1, $zero # Initialize a
j increment_a

check_remainder:
mfhi $t5 # remainder
bnez $t5, increment_a
sub $t3, $a0, $t1
sub $t3, $t3, $t2 # c = N - a - b

sw $t1, 0($a1)
sw $t2, 0($a2)
sw $t3, 0($a3)
addi $a1, $a1, 4 # Increment pointer for a
addi $a2, $a2, 4 # Increment pointer for b
addi $a3, $a3, 4 # Increment pointer for c

increment_a:
addi $t1, $t1, 1
sub $t7, $a0, $t1 # N - a
sub $t6, $t7, $t1 # N - 2*a
sll $t7, $t7, 1 # denominator = 2*N - 2*a
mulu $t6, $a0, $t6 # numerator = N² - 2*N*a
div $t6, $t7
mflo $t2 # b
bgt $t2, $t1, check_remainder # loop while b > a

return:
sub $v0, $a1, $t0 # compute number of triplets
srl $v0, $v0, 2
jr $ra
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"
13 changes: 13 additions & 0 deletions exercises/practice/pythagorean-triplet/impl.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# | Register | Usage | Type | Description |
# | -------- | ------------ | ------- | -------------------------------------------- |
# | `$a0` | input | integer | N, sum of sides of triangle |
# | `$a1` | input/output | address | a values |
# | `$a2` | input/output | address | b values |
# | `$a3` | input/output | address | c values |
# | `$v0` | output | integer | number of triplets |
# | `$t0-9` | temporary | any | for temporary storage |

.globl triplets_with_sum

triplets_with_sum:
jr $ra
Loading