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 square-root exercise #181

Merged
merged 1 commit into from
Oct 13, 2023
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
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@
"strings"
]
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "8dcd4afd-7ef8-4bd1-85e1-b070357c970a",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"topics": [
"conditionals",
"loops",
"integers",
"math"
]
},
{
"slug": "resistor-color",
"name": "Resistor Color",
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
2 changes: 2 additions & 0 deletions exercises/practice/square-root/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.o
tests
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.asm"
],
"test": [
"square_root_test.c"
],
"example": [
".meta/example.asm"
]
},
"blurb": "Given a natural radicand, return its square root.",
"source": "wolf99",
"source_url": "https://github.com/exercism/problem-specifications/pull/1582"
}
31 changes: 31 additions & 0 deletions exercises/practice/square-root/.meta/example.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
;
; Calculate integer square root
;
; Parameter:
; rdi - radicand
; Returns:
; rax - square root
;
section .text
global square_root
square_root:
; rdx - current square number, n*n
; rax - current odd number, 2n+1
xor rdx, rdx ; Current square number is 0
mov rax, 1 ; Current odd number is 1
jmp .compare

.advance:
add rdx, rax ; n*n+2n+1 == (n+1)*(n+1)
add rax, 2 ; 2n+1+2 == 2(n+1)+1

.compare:
cmp rdx, rdi ; Compare current square with radicand
jb .advance

shr rax, 1 ; Halve rax (rounding down) to find n
ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
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"
46 changes: 46 additions & 0 deletions exercises/practice/square-root/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
AS = nasm

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =
ASFLAGS = -g -F dwarf -Werror

ifeq ($(shell uname -s),Darwin)
ifeq ($(shell sysctl -n hw.optional.arm64 2>/dev/null),1)
ALL_CFLAGS = -target x86_64-apple-darwin
endif
ALL_LDFLAGS = -Wl,-pie -Wl,-fatal_warnings
ALL_ASFLAGS = -f macho64 --prefix _
else
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
ALL_ASFLAGS = -f elf64
endif

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

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

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

all: tests
@./$<

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

%.o: %.asm
@$(AS) $(ALL_ASFLAGS) -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
9 changes: 9 additions & 0 deletions exercises/practice/square-root/square_root.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
section .text
global square_root
square_root:
; Provide your implementation here
ret

%ifidn __OUTPUT_FORMAT__,elf64
section .note.GNU-stack noalloc noexec nowrite progbits
%endif
51 changes: 51 additions & 0 deletions exercises/practice/square-root/square_root_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Version: 1.0.0

#include "vendor/unity.h"

extern int square_root(int 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));
}

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);
return UNITY_END();
}
Loading