Gets the n-th prime number. input: positive integer 'n' >= 0 returns the n-th prime number, beginning at index 0
(n)
| 436 | |
| 437 | |
| 438 | def getPrime(n): |
| 439 | """ |
| 440 | Gets the n-th prime number. |
| 441 | input: positive integer 'n' >= 0 |
| 442 | returns the n-th prime number, beginning at index 0 |
| 443 | """ |
| 444 | |
| 445 | # precondition |
| 446 | assert isinstance(n, int) and (n >= 0), "'number' must been a positive int" |
| 447 | |
| 448 | index = 0 |
| 449 | ans = 2 # this variable holds the answer |
| 450 | |
| 451 | while index < n: |
| 452 | index += 1 |
| 453 | |
| 454 | ans += 1 # counts to the next number |
| 455 | |
| 456 | # if ans not prime then |
| 457 | # runs to the next prime number. |
| 458 | while not isPrime(ans): |
| 459 | ans += 1 |
| 460 | |
| 461 | # precondition |
| 462 | assert isinstance(ans, int) and isPrime(ans), ( |
| 463 | "'ans' must been a prime number and from type int" |
| 464 | ) |
| 465 | |
| 466 | return ans |
| 467 | |
| 468 | |
| 469 | # --------------------------------------------------- |