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 #405

Merged
merged 1 commit into from
Oct 6, 2024
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 @@ -230,6 +230,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "ae46d5df-62cc-46d8-a773-bc93a1ad716e",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "nucleotide-count",
"name": "Nucleotide Count",
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": [
"BNAndras"
],
"files": {
"solution": [
"nth-prime.rkt"
],
"test": [
"nth-prime-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"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"
}
29 changes: 29 additions & 0 deletions exercises/practice/nth-prime/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#lang racket

(provide nth-prime)

(define/contract (nth-prime number)
(-> exact-positive-integer? exact-positive-integer?)
(let loop ([candidate 2]
[count 0])
(cond
[(prime? candidate)
(cond
[(= (add1 count) number) candidate]
[else (loop (add1 candidate)
(add1 count))])]
[else (loop (add1 candidate)
count)])))

(define (prime? number)
(cond
[(zero? number) #f]
[(= number 1) #f]
[(= number 2) #t]
[(even? number) #f]
[else
(let loop ([factor 3])
(cond
[(> (expt factor 2) number) #t]
[(zero? (remainder number factor)) #f]
[else (loop (+ factor 2))]))]))
25 changes: 25 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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"
26 changes: 26 additions & 0 deletions exercises/practice/nth-prime/nth-prime-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#lang racket/base

(require "nth-prime.rkt")

(module+ test
(require rackunit rackunit/text-ui)

(define suite
(test-suite
"nth prime tests"
(test-equal? "first prime"
(nth-prime 1)
2)
(test-equal? "second prime"
(nth-prime 2)
3)
(test-equal? "sixth prime"
(nth-prime 6)
13)
(test-equal? "big prime"
(nth-prime 10001)
104743)
(test-exn "there is no zeroth prime"
exn:fail? (lambda () (nth-prime 0)))))

(run-tests suite))
6 changes: 6 additions & 0 deletions exercises/practice/nth-prime/nth-prime.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#lang racket

(provide nth-prime)

(define (nth-prime number)
(error "Not implemented yet"))