Skip to content

Commit

Permalink
feat: Add Square Root (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 13, 2023
1 parent fd936a9 commit 8568cc1
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
"math"
]
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "f2c63a16-4dbe-4994-a54a-b4113caab55c",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"topics": [
"math"
]
},
{
"slug": "trinary",
"name": "Trinary",
Expand Down
13 changes: 13 additions & 0 deletions exercises/practice/square-root/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Given a natural radicand, return its square root.

Note that the term "radicand" refers to the number for which the root is to be determined.
That is, it is the number under the root symbol.

Check out the Wikipedia pages on [square root][square-root] and [methods of computing square roots][computing-square-roots].

Recall also that natural numbers are positive real whole numbers (i.e. 1, 2, 3 and up).

[square-root]: https://en.wikipedia.org/wiki/Square_root
[computing-square-roots]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
20 changes: 20 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"keiravillekode"
],
"contributors": [],
"files": {
"solution": [
"impl.mips"
],
"test": [
"runner.mips"
],
"example": [
".meta/example.mips"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
25 changes: 25 additions & 0 deletions exercises/practice/square-root/.meta/example.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Calculate integer square_root
#
# a0 - input radicand
# t0 - current square number, n*n
# t1 - next triangular number, 2n+1
# v0 - output word

.globl square_root

.text

square_root:
move $t0, $zero # 0*0 == 0
li $t1, 1 # 2*0+1 == 1
j compare

advance:
add $t0, $t0, $t1 # n*n+2n+1 == (n+1)*(n+1)
addi $t1, $t1, 2 # 2n+1+2 == 2(n+1)+1

compare:
blt $t0, $a0, advance
srl $v0, $t1, 1 # Halve 2n+1, rounding down
jr $ra
28 changes: 28 additions & 0 deletions exercises/practice/square-root/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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.

[9b748478-7b0a-490c-b87a-609dacf631fd]
description = "root of 1"

[7d3aa9ba-9ac6-4e93-a18b-2e8b477139bb]
description = "root of 4"

[6624aabf-3659-4ae0-a1c8-25ae7f33c6ef]
description = "root of 25"

[93beac69-265e-4429-abb1-94506b431f81]
description = "root of 81"

[fbddfeda-8c4f-4bc4-87ca-6991af35360e]
description = "root of 196"

[c03d0532-8368-4734-a8e0-f96a9eb7fc1d]
description = "root of 65025"
Empty file.
75 changes: 75 additions & 0 deletions exercises/practice/square-root/runner.mips
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#
# Test square_root with some examples
#
# s0 - num of tests left to run
# s1 - address of input word
# s2 - address of expected output word
# s4 - output word
#
# square_root must:
# - be named square_root and declared as global
# - read radicand from a0
# - follow the convention of using the t0-9 registers for temporary storage
# - (if it uses s0-7 then it is responsible for pushing existing values to the stack then popping them back off before returning)
# - write integer result to v0

.data

# number of test cases
n: .word 6
# input values (null terminated) & expected output values (word sized ints)
ins: .word 1, 4, 25, 81, 196, 65025

outs: .word 1, 2, 5, 9, 14, 255

failmsg: .asciiz "failed for test input: "
okmsg: .asciiz "all tests passed"


.text

runner:
lw $s0, n
la $s1, ins
la $s2, outs
j run_test

run_test:
lw $a0, 0($s1) # move input number to a0
jal square_root # call subroutine under test
move $v1, $v0 # move return value in v0 to v1 because we need v0 for syscall

lw $s4, 0($s2) # read expected output from memory
bne $v1, $s4, exit_fail # if expected doesn't match actual, jump to fail

addi $s1, $s1, 4 # move to next word in input
addi $s2, $s2, 4 # move to next word in output

sub $s0, $s0, 1 # decrement num of tests left to run
bgt $s0, $zero, run_test # if more than zero tests to run, jump to run_test

exit_ok:
la $a0, okmsg # put address of okmsg into a0
li $v0, 4 # 4 is print string
syscall

li $v0, 10 # 10 is exit with zero status (clean exit)
syscall

exit_fail:
la $a0, failmsg # put address of failmsg into a0
li $v0, 4 # 4 is print string
syscall

lw $a0, ($s1) # print input that failed on
li $v0, 1
syscall

li $a0, 1 # set error code to 1
li $v0, 17 # 17 is exit with error
syscall

jr $ra

# # Include your implementation here if you wish to run this from the MARS GUI.
# .include "impl.mips"

0 comments on commit 8568cc1

Please sign in to comment.