Skip to content

Commit

Permalink
Add square-root exercise (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 19, 2024
1 parent 512c284 commit 0b77765
Show file tree
Hide file tree
Showing 12 changed files with 3,510 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "fed39aff-8fc2-49a1-97fa-84fe1646fd60",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
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
19 changes: 19 additions & 0 deletions exercises/practice/square-root/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"square_root.s"
],
"test": [
"square_root_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
18 changes: 18 additions & 0 deletions exercises/practice/square-root/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.text
.globl square_root

/* extern uint64_t square_root(uint64_t radicand); */
square_root:
mov x1, x0 /* radicand */
mov x0, #1 /* initial guess */

.loop:
mov x2, x0 /* current guess */
add x3, x2, #1
madd x3, x3, x2, x1 /* guess (guess + 1) + radicand */
lsl x4, x2, #1 /* 2 * guess */
udiv x0, x3, x4 /* new guess */
cmp x0, x2
bne .loop

ret
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"
36 changes: 36 additions & 0 deletions exercises/practice/square-root/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/square-root/square_root.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl square_root

square_root:
ret
57 changes: 57 additions & 0 deletions exercises/practice/square-root/square_root_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "vendor/unity.h"

#include <stdint.h>

extern uint64_t square_root(uint64_t radicand);

void setUp(void) {
}

void tearDown(void) {
}

void test_root_of_1(void) {
TEST_ASSERT_EQUAL_INT(1, square_root(1));
}

void test_root_of_4(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(2, square_root(4));
}

void test_root_of_25(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(5, square_root(25));
}

void test_root_of_81(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(9, square_root(81));
}

void test_root_of_196(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(14, square_root(196));
}

void test_root_of_65025(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(255, square_root(65025));
}

void test_root_of_4905601600(void) {
TEST_IGNORE();
TEST_ASSERT_EQUAL_INT(70040, square_root(4905601600));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_root_of_1);
RUN_TEST(test_root_of_4);
RUN_TEST(test_root_of_25);
RUN_TEST(test_root_of_81);
RUN_TEST(test_root_of_196);
RUN_TEST(test_root_of_65025);
RUN_TEST(test_root_of_4905601600);
return UNITY_END();
}
Loading

0 comments on commit 0b77765

Please sign in to comment.