main.hs 690 B

1234567891011121314151617181920
  1. nthPrime :: Integer -> Integer
  2. nthPrime targetCount = nextPrimeImpl 2 1 []
  3. where
  4. nextPrimeImpl i primeCount primeList =
  5. if isPrime then
  6. if primeCount == targetCount then
  7. i
  8. else
  9. nextPrimeImpl (i + 1) (primeCount + 1) (primeList ++ [i])
  10. else
  11. nextPrimeImpl (i+1) primeCount primeList
  12. where
  13. isPrime =
  14. isPrimeImpl primeList
  15. where
  16. isPrimeImpl [] = True
  17. isPrimeImpl (x:xs) = i `mod` x /= 0 && isPrimeImpl xs
  18. main = do
  19. putStrLn $ show $ nthPrime 10001