-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pythagorean-triplet exercise (#263)
- Loading branch information
1 parent
1c3b0e8
commit 8ed14cb
Showing
9 changed files
with
427 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
12 changes: 12 additions & 0 deletions
12
exercises/practice/pythagorean-triplet/.docs/instructions.append.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
exercises/practice/pythagorean-triplet/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.