Skip to content

Commit c8c05d6

Browse files
Add nth-prime
1 parent ed676d3 commit c8c05d6

File tree

12 files changed

+3526
-0
lines changed

12 files changed

+3526
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@
303303
"prerequisites": [],
304304
"difficulty": 6
305305
},
306+
{
307+
"slug": "nth-prime",
308+
"name": "Nth Prime",
309+
"uuid": "ba707f5a-61ba-45dd-a5bb-b2b573753f91",
310+
"practices": [],
311+
"prerequisites": [],
312+
"difficulty": 6
313+
},
306314
{
307315
"slug": "pascals-triangle",
308316
"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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.text
2+
.globl prime
3+
4+
/*
5+
| Register | Usage | Type | Description |
6+
| -------- | ---------- | ------- | ----------------------- |
7+
| `a0` | output | integer | prime |
8+
| `a0` | input | integer | remaining primes |
9+
| `a1` | temporary | integer | 6 |
10+
| `t0` | temporary | integer | prime candidate |
11+
| `t1` | temporary | integer | prime step |
12+
| `t2` | temporary | integer | factor candidate |
13+
| `t3` | temporary | integer | factor step |
14+
*/
15+
16+
/* extern unsigned prime(unsigned number); */
17+
prime:
18+
li a1, 6
19+
li t1, 4 /* prime step */
20+
li t0, 1 /* prime candidate will be 5 */
21+
addi a0, a0, -2
22+
bgtz a0, outer
23+
24+
addi a0, a0, 3 /* Prime 1 is 2, Prime 2 is 3 */
25+
ret
26+
27+
found_prime:
28+
addi a0, a0, -1
29+
bnez a0, outer
30+
move a0, t0
31+
ret
32+
33+
outer:
34+
add t0, t0, t1 /* prime candidate */
35+
sub t1, a1, t1 /* prime step */
36+
li t3, 4 /* factor step */
37+
li t2, 1 /* factor candidate will be 5 */
38+
39+
inner:
40+
add t2, t2, t3 /* factor candidate */
41+
sub t3, a1, t3 /* factor step */
42+
mul t4, t2, t2
43+
bgt t4, t0, found_prime
44+
rem t5, t0, t2
45+
bnez t5, inner
46+
j outer
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
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
AS = /usr/bin/riscv64-linux-gnu-as
2+
CC = /usr/bin/riscv64-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-riscv64 -L /usr/riscv64-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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "vendor/unity.h"
2+
3+
extern unsigned prime(unsigned number);
4+
5+
void setUp(void) {
6+
}
7+
8+
void tearDown(void) {
9+
}
10+
11+
void test_first_prime(void) {
12+
TEST_ASSERT_EQUAL_UINT(2, prime(1));
13+
}
14+
15+
void test_second_prime(void) {
16+
TEST_IGNORE();
17+
TEST_ASSERT_EQUAL_UINT(3, prime(2));
18+
}
19+
20+
void test_sixth_prime(void) {
21+
TEST_IGNORE();
22+
TEST_ASSERT_EQUAL_UINT(13, prime(6));
23+
}
24+
25+
void test_big_prime(void) {
26+
TEST_IGNORE();
27+
TEST_ASSERT_EQUAL_UINT(104743, prime(10001));
28+
}
29+
30+
void test_seventh_prime(void) {
31+
TEST_IGNORE();
32+
TEST_ASSERT_EQUAL_UINT(17, prime(7));
33+
}
34+
35+
void test_very_big_prime(void) {
36+
TEST_IGNORE();
37+
TEST_ASSERT_EQUAL_UINT(821647, prime(65537));
38+
}
39+
40+
int main(void) {
41+
UNITY_BEGIN();
42+
RUN_TEST(test_first_prime);
43+
RUN_TEST(test_second_prime);
44+
RUN_TEST(test_sixth_prime);
45+
RUN_TEST(test_big_prime);
46+
RUN_TEST(test_seventh_prime);
47+
RUN_TEST(test_very_big_prime);
48+
return UNITY_END();
49+
}

0 commit comments

Comments
 (0)