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 nth-prime exercise #443

Merged
merged 1 commit into from
Jan 30, 2025
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "799a17cb-9f29-481b-94be-b532f46c8024",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "pangram",
"name": "Pangram",
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/nth-prime/.docs/instructions.md
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.
19 changes: 19 additions & 0 deletions exercises/practice/nth-prime/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"nth_prime.zig"
],
"test": [
"test_nth_prime.zig"
],
"example": [
".meta/example.zig"
]
},
"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"
}
37 changes: 37 additions & 0 deletions exercises/practice/nth-prime/.meta/example.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const std = @import("std");
const math = std.math;
const mem = std.mem;

pub fn prime(allocator: mem.Allocator, number: usize) !usize {
// The first two primes are 2 and 3.
if (number <= 2) {
return number + 1;
}

const limit = number * math.log2_int_ceil(usize, number);
const table = try allocator.alloc(bool, limit);
defer allocator.free(table);
@memset(table, false);

var remaining = number - 2;
var p: usize = 1;
var step: usize = 4;
while (true) {
p += step; // skip multiples of 2 and multiples of 3
step = 6 - step;
if (table[p]) {
continue;
}

remaining -= 1;
if (remaining == 0) {
return p;
}

var q = p * p;
while (q < limit) {
table[q] = true;
q += 2 * p;
}
}
}
26 changes: 26 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
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
8 changes: 8 additions & 0 deletions exercises/practice/nth-prime/nth_prime.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const std = @import("std");
const mem = std.mem;

pub fn prime(allocator: mem.Allocator, number: usize) !usize {
_ = allocator;
_ = number;
@compileError("please implement the prime function");
}
44 changes: 44 additions & 0 deletions exercises/practice/nth-prime/test_nth_prime.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const std = @import("std");
const testing = std.testing;

const nth_prime = @import("nth_prime.zig");

test "first prime" {
const p = try nth_prime.prime(testing.allocator, 1);
try testing.expectEqual(2, p);
}

test "second prime" {
const p = try nth_prime.prime(testing.allocator, 2);
try testing.expectEqual(3, p);
}

test "third prime" {
const p = try nth_prime.prime(testing.allocator, 3);
try testing.expectEqual(5, p);
}

test "fourth prime" {
const p = try nth_prime.prime(testing.allocator, 4);
try testing.expectEqual(7, p);
}

test "fifth prime" {
const p = try nth_prime.prime(testing.allocator, 5);
try testing.expectEqual(11, p);
}

test "sixth prime" {
const p = try nth_prime.prime(testing.allocator, 6);
try testing.expectEqual(13, p);
}

test "seventh prime" {
const p = try nth_prime.prime(testing.allocator, 7);
try testing.expectEqual(17, p);
}

test "big prime" {
const p = try nth_prime.prime(testing.allocator, 10001);
try testing.expectEqual(104743, p);
}