fork download
  1. object Main extends App {
  2. import scala.annotation.tailrec
  3. @tailrec final def nextPrime(i: BigInt): BigInt =
  4. if (i.isProbablePrime(95)) {
  5. i
  6. } else {
  7. nextPrime(i + 1)
  8. }
  9.  
  10. @tailrec final def nthPrimeHelper(n: Int, curr: BigInt): BigInt =
  11. if (n == 0) {
  12. curr
  13. } else {
  14. nthPrimeHelper(n - 1, nextPrime(curr + BigInt(1)))
  15. }
  16.  
  17. def nthPrime(n: Int) = nthPrimeHelper(n, 2)
  18.  
  19. (0 to 10) foreach { case n =>
  20. println(nthPrime(n))
  21. }
  22. }
Success #stdin #stdout 0.44s 382144KB
stdin
Standard input is empty
stdout
2
3
5
7
11
13
17
19
23
29
31