Gets the n-th prime number. input: positive integer 'n' >= 0 returns the n-th prime number, beginning at index 0 >>> get_prime(0) 2 >>> get_prime(8) 23 >>> get_prime(824) 6337 >>> get_prime(-1) Traceback (most recent call last): ... Assertion
(n)
| 536 | |
| 537 | |
| 538 | def get_prime(n): |
| 539 | """ |
| 540 | Gets the n-th prime number. |
| 541 | input: positive integer 'n' >= 0 |
| 542 | returns the n-th prime number, beginning at index 0 |
| 543 | |
| 544 | >>> get_prime(0) |
| 545 | 2 |
| 546 | >>> get_prime(8) |
| 547 | 23 |
| 548 | >>> get_prime(824) |
| 549 | 6337 |
| 550 | >>> get_prime(-1) |
| 551 | Traceback (most recent call last): |
| 552 | ... |
| 553 | AssertionError: 'number' must been a positive int |
| 554 | >>> get_prime("test") |
| 555 | Traceback (most recent call last): |
| 556 | ... |
| 557 | AssertionError: 'number' must been a positive int |
| 558 | """ |
| 559 | |
| 560 | # precondition |
| 561 | assert isinstance(n, int) and (n >= 0), "'number' must been a positive int" |
| 562 | |
| 563 | index = 0 |
| 564 | ans = 2 # this variable holds the answer |
| 565 | |
| 566 | while index < n: |
| 567 | index += 1 |
| 568 | |
| 569 | ans += 1 # counts to the next number |
| 570 | |
| 571 | # if ans not prime then |
| 572 | # runs to the next prime number. |
| 573 | while not is_prime(ans): |
| 574 | ans += 1 |
| 575 | |
| 576 | # precondition |
| 577 | assert isinstance(ans, int) and is_prime(ans), ( |
| 578 | "'ans' must been a prime number and from type int" |
| 579 | ) |
| 580 | |
| 581 | return ans |
| 582 | |
| 583 | |
| 584 | # --------------------------------------------------- |
nothing calls this directly
no test coverage detected