Skip to content

Instantly share code, notes, and snippets.

@DrunkenAlcoholic
Created May 14, 2024 04:11
Show Gist options
  • Save DrunkenAlcoholic/3e142f8c216cba9509a35b94e6822240 to your computer and use it in GitHub Desktop.
Save DrunkenAlcoholic/3e142f8c216cba9509a35b94e6822240 to your computer and use it in GitHub Desktop.
Nth Prime [Exercism - Nim]
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