Skip to content

Commit cae5e6c

Browse files
Add nth-prime exercise (#110)
1 parent bc11ff5 commit cae5e6c

File tree

12 files changed

+3542
-0
lines changed

12 files changed

+3542
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@
306306
"prerequisites": [],
307307
"difficulty": 6
308308
},
309+
{
310+
"slug": "nth-prime",
311+
"name": "Nth Prime",
312+
"uuid": "2008083a-fdc5-4a14-8668-6e58f7cd187e",
313+
"practices": [],
314+
"prerequisites": [],
315+
"difficulty": 6
316+
},
309317
{
310318
"slug": "pascals-triangle",
311319
"name": "Pascal's Triangle",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Given a number n, determine what the nth prime is.
4+
5+
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
6+
7+
If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"nth_prime.s"
8+
],
9+
"test": [
10+
"nth_prime_test.c"
11+
],
12+
"example": [
13+
".meta/example.s"
14+
]
15+
},
16+
"blurb": "Given a number n, determine what the nth prime is.",
17+
"source": "A variation on Problem 7 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=7"
19+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
.text
2+
.globl prime
3+
4+
/* extern uint64_t prime(uint64_t number); */
5+
prime:
6+
cmp x0, #3
7+
blt .small
8+
9+
clz x1, x0 /* count leading zero bits */
10+
mov x2, #64
11+
sub x1, x2, x1 /* ceil(log2(number + 1)) */
12+
mul x1, x0, x1 /* number * ceil(log2(number + 1)) */
13+
lsr x1, x1, #1 /* divide by 2 */
14+
add x1, x1, #15
15+
and x1, x1, #-16 /* round up to multiple of 16 */
16+
mov x3, sp
17+
sub sp, sp, x1 /* allocate space on stack */
18+
mov x4, #-1
19+
mov x5, sp
20+
21+
.fill:
22+
stp x4, x4, [x3, #-16]! /* store pair, pre-decrement */
23+
cmp x3, x5
24+
bne .fill
25+
26+
mov x4, #1
27+
sub x0, x0, #1 /* we skip the prime 2 */
28+
29+
.search:
30+
add x4, x4, #2 /* candidate prime */
31+
lsr x6, x4, #1 /* divide by 2 */
32+
ldrb w5, [sp, x6]
33+
cbz w5, .search /* if composite, move on to next candidate */
34+
35+
sub x0, x0, #1 /* number of primes required */
36+
cbz x0, .exit
37+
38+
mul x5, x4, x4 /* multiple of prime */
39+
lsr x5, x5, #1 /* divide by 2 */
40+
41+
.mark:
42+
cmp x5, x1
43+
bhs .search /* unsigned >= */
44+
45+
strb wzr, [sp, x5] /* mark as composite */
46+
add x5, x5, x4
47+
b .mark
48+
49+
.exit:
50+
mov x0, x4
51+
add sp, sp, x1 /* restore original stack pointer */
52+
ret
53+
54+
.small:
55+
add x0, x0, #1
56+
ret
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[75c65189-8aef-471a-81de-0a90c728160c]
13+
description = "first prime"
14+
15+
[2c38804c-295f-4701-b728-56dea34fd1a0]
16+
description = "second prime"
17+
18+
[56692534-781e-4e8c-b1f9-3e82c1640259]
19+
description = "sixth prime"
20+
21+
[fce1e979-0edb-412d-93aa-2c744e8f50ff]
22+
description = "big prime"
23+
24+
[bd0a9eae-6df7-485b-a144-80e13c7d55b2]
25+
description = "there is no zeroth prime"
26+
include = false

exercises/practice/nth-prime/Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = aarch64-linux-gnu-as
2+
CC = aarch64-linux-gnu-gcc
3+
4+
CFLAGS = -g -Wall -Wextra -pedantic -Werror
5+
LDFLAGS =
6+
7+
ALL_LDFLAGS = -pie -Wl,--fatal-warnings
8+
9+
ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
10+
ALL_LDFLAGS += $(LDFLAGS)
11+
12+
C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
13+
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
14+
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)
15+
16+
CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<
17+
18+
all: tests
19+
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<
20+
21+
tests: $(ALL_OBJS)
22+
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)
23+
24+
%.o: %.s
25+
@$(AS) -o $@ $<
26+
27+
%.o: %.c
28+
@$(CC_CMD)
29+
30+
vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
31+
@$(CC_CMD)
32+
33+
clean:
34+
@rm -f *.o vendor/*.o tests
35+
36+
.PHONY: all clean
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.text
2+
.globl prime
3+
4+
prime:
5+
ret
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "vendor/unity.h"
2+
3+
#include <stdint.h>
4+
5+
extern uint64_t prime(uint64_t number);
6+
7+
void setUp(void) {
8+
}
9+
10+
void tearDown(void) {
11+
}
12+
13+
void test_first_prime(void) {
14+
TEST_ASSERT_EQUAL_UINT(2, prime(1));
15+
}
16+
17+
void test_second_prime(void) {
18+
TEST_IGNORE();
19+
TEST_ASSERT_EQUAL_UINT(3, prime(2));
20+
}
21+
22+
void test_sixth_prime(void) {
23+
TEST_IGNORE();
24+
TEST_ASSERT_EQUAL_UINT(13, prime(6));
25+
}
26+
27+
void test_big_prime(void) {
28+
TEST_IGNORE();
29+
TEST_ASSERT_EQUAL_UINT(104743, prime(10001));
30+
}
31+
32+
void test_seventh_prime(void) {
33+
TEST_IGNORE();
34+
TEST_ASSERT_EQUAL_UINT(17, prime(7));
35+
}
36+
37+
void test_very_big_prime(void) {
38+
TEST_IGNORE();
39+
TEST_ASSERT_EQUAL_UINT(821647, prime(65537));
40+
}
41+
42+
int main(void) {
43+
UNITY_BEGIN();
44+
RUN_TEST(test_first_prime);
45+
RUN_TEST(test_second_prime);
46+
RUN_TEST(test_sixth_prime);
47+
RUN_TEST(test_big_prime);
48+
RUN_TEST(test_seventh_prime);
49+
RUN_TEST(test_very_big_prime);
50+
return UNITY_END();
51+
}

0 commit comments

Comments
 (0)