MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / getPrime

Function getPrime

other/primelib.py:418–446  ·  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

416# ----------------------------------
417
418def getPrime(n):
419 """
420 Gets the n-th prime number.
421 input: positive integer 'n' >= 0
422 returns the n-th prime number, beginning at index 0
423 """
424
425 # precondition
426 assert isinstance(n,int) and (n >= 0), "'number' must been a positive int"
427
428 index = 0
429 ans = 2 # this variable holds the answer
430
431 while index < n:
432
433 index += 1
434
435 ans += 1 # counts to the next number
436
437 # if ans not prime then
438 # runs to the next prime number.
439 while not isPrime(ans):
440 ans += 1
441
442 # precondition
443 assert isinstance(ans,int) and isPrime(ans), \
444 "'ans' must been a prime number and from type int"
445
446 return ans
447
448# ---------------------------------------------------
449

Callers

nothing calls this directly

Calls 1

isPrimeFunction · 0.70

Tested by

no test coverage detected