-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc11ff5
commit cae5e6c
Showing
12 changed files
with
3,542 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,7 @@ | ||
# Instructions | ||
|
||
Given a number n, determine what the nth prime is. | ||
|
||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. | ||
|
||
If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself. |
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": [ | ||
"nth_prime.s" | ||
], | ||
"test": [ | ||
"nth_prime_test.c" | ||
], | ||
"example": [ | ||
".meta/example.s" | ||
] | ||
}, | ||
"blurb": "Given a number n, determine what the nth prime is.", | ||
"source": "A variation on Problem 7 at Project Euler", | ||
"source_url": "https://projecteuler.net/problem=7" | ||
} |
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,56 @@ | ||
.text | ||
.globl prime | ||
|
||
/* extern uint64_t prime(uint64_t number); */ | ||
prime: | ||
cmp x0, #3 | ||
blt .small | ||
|
||
clz x1, x0 /* count leading zero bits */ | ||
mov x2, #64 | ||
sub x1, x2, x1 /* ceil(log2(number + 1)) */ | ||
mul x1, x0, x1 /* number * ceil(log2(number + 1)) */ | ||
lsr x1, x1, #1 /* divide by 2 */ | ||
add x1, x1, #15 | ||
and x1, x1, #-16 /* round up to multiple of 16 */ | ||
mov x3, sp | ||
sub sp, sp, x1 /* allocate space on stack */ | ||
mov x4, #-1 | ||
mov x5, sp | ||
|
||
.fill: | ||
stp x4, x4, [x3, #-16]! /* store pair, pre-decrement */ | ||
cmp x3, x5 | ||
bne .fill | ||
|
||
mov x4, #1 | ||
sub x0, x0, #1 /* we skip the prime 2 */ | ||
|
||
.search: | ||
add x4, x4, #2 /* candidate prime */ | ||
lsr x6, x4, #1 /* divide by 2 */ | ||
ldrb w5, [sp, x6] | ||
cbz w5, .search /* if composite, move on to next candidate */ | ||
|
||
sub x0, x0, #1 /* number of primes required */ | ||
cbz x0, .exit | ||
|
||
mul x5, x4, x4 /* multiple of prime */ | ||
lsr x5, x5, #1 /* divide by 2 */ | ||
|
||
.mark: | ||
cmp x5, x1 | ||
bhs .search /* unsigned >= */ | ||
|
||
strb wzr, [sp, x5] /* mark as composite */ | ||
add x5, x5, x4 | ||
b .mark | ||
|
||
.exit: | ||
mov x0, x4 | ||
add sp, sp, x1 /* restore original stack pointer */ | ||
ret | ||
|
||
.small: | ||
add x0, x0, #1 | ||
ret |
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,26 @@ | ||
# 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. | ||
|
||
[75c65189-8aef-471a-81de-0a90c728160c] | ||
description = "first prime" | ||
|
||
[2c38804c-295f-4701-b728-56dea34fd1a0] | ||
description = "second prime" | ||
|
||
[56692534-781e-4e8c-b1f9-3e82c1640259] | ||
description = "sixth prime" | ||
|
||
[fce1e979-0edb-412d-93aa-2c744e8f50ff] | ||
description = "big prime" | ||
|
||
[bd0a9eae-6df7-485b-a144-80e13c7d55b2] | ||
description = "there is no zeroth prime" | ||
include = false |
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,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 |
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,5 @@ | ||
.text | ||
.globl prime | ||
|
||
prime: | ||
ret |
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,51 @@ | ||
#include "vendor/unity.h" | ||
|
||
#include <stdint.h> | ||
|
||
extern uint64_t prime(uint64_t number); | ||
|
||
void setUp(void) { | ||
} | ||
|
||
void tearDown(void) { | ||
} | ||
|
||
void test_first_prime(void) { | ||
TEST_ASSERT_EQUAL_UINT(2, prime(1)); | ||
} | ||
|
||
void test_second_prime(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT(3, prime(2)); | ||
} | ||
|
||
void test_sixth_prime(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT(13, prime(6)); | ||
} | ||
|
||
void test_big_prime(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT(104743, prime(10001)); | ||
} | ||
|
||
void test_seventh_prime(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT(17, prime(7)); | ||
} | ||
|
||
void test_very_big_prime(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT(821647, prime(65537)); | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
RUN_TEST(test_first_prime); | ||
RUN_TEST(test_second_prime); | ||
RUN_TEST(test_sixth_prime); | ||
RUN_TEST(test_big_prime); | ||
RUN_TEST(test_seventh_prime); | ||
RUN_TEST(test_very_big_prime); | ||
return UNITY_END(); | ||
} |
Oops, something went wrong.