MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_007/sol1.py:52–80  ·  view source on GitHub ↗

Returns the n-th prime number. >>> solution(6) 13 >>> solution(1) 2 >>> solution(3) 5 >>> solution(20) 71 >>> solution(50) 229 >>> solution(100) 541

(nth: int = 10001)

Source from the content-addressed store, hash-verified

50
51
52def solution(nth: int = 10001) -> int:
53 """
54 Returns the n-th prime number.
55
56 >>> solution(6)
57 13
58 >>> solution(1)
59 2
60 >>> solution(3)
61 5
62 >>> solution(20)
63 71
64 >>> solution(50)
65 229
66 >>> solution(100)
67 541
68 """
69
70 count = 0
71 number = 1
72 while count != nth and number < 3:
73 number += 1
74 if is_prime(number):
75 count += 1
76 while count != nth:
77 number += 2
78 if is_prime(number):
79 count += 1
80 return number
81
82
83if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

is_primeFunction · 0.70

Tested by

no test coverage detected