MCPcopy Index your code
hub / github.com/geekcomputers/Python / getPrime

Function getPrime

primelib/primelib.py:438–466  ·  view source on GitHub ↗

Gets the n-th prime number. input: positive integer 'n' >= 0 returns the n-th prime number, beginning at index 0

(n)

Source from the content-addressed store, hash-verified

436
437
438def 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# ---------------------------------------------------

Callers

nothing calls this directly

Calls 1

isPrimeFunction · 0.85

Tested by

no test coverage detected