Created
May 14, 2024 04:11
-
-
Save DrunkenAlcoholic/3e142f8c216cba9509a35b94e6822240 to your computer and use it in GitHub Desktop.
Nth Prime [Exercism - Nim]
This file contains hidden or 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
import std/math | |
proc isPrime(number: int): bool= | |
let squareRoot = int(sqrt(float(number))) | |
for i in 2..squareRoot+1: | |
if number mod i == 0: return false | |
return true | |
proc prime*(n: int): int = | |
if n < 1: raise newException(ValueError, "Invalid input") | |
if n == 1: return 2 | |
var | |
primeCount = 0 | |
primeNumber = 1 | |
while primeCount < n: | |
if isPrime(primeNumber): | |
result = primeNumber | |
inc(primeCount) | |
inc(primeNumber) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment