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

Function solution

A solution to project euler problem 3.py:11–62  ·  view source on GitHub ↗

Returns the largest prime factor of a given number n. >>> solution(13195) 29 >>> solution(10) 5 >>> solution(17) 17 >>> solution(3.4) 3 >>> solution(0) Traceback (most recent call last): ... ValueError: Parameter n must be greater or equal to one.

(n: int = 600851475143)

Source from the content-addressed store, hash-verified

9
10# def solution(n: int) -> int:
11def solution(n: int = 600851475143) -> int:
12 """Returns the largest prime factor of a given number n.
13 >>> solution(13195)
14 29
15 >>> solution(10)
16 5
17 >>> solution(17)
18 17
19 >>> solution(3.4)
20 3
21 >>> solution(0)
22 Traceback (most recent call last):
23 ...
24 ValueError: Parameter n must be greater or equal to one.
25 >>> solution(-17)
26 Traceback (most recent call last):
27 ...
28 ValueError: Parameter n must be greater or equal to one.
29 >>> solution([])
30 Traceback (most recent call last):
31 ...
32 TypeError: Parameter n must be int or passive of cast to int.
33 >>> solution("asd")
34 Traceback (most recent call last):
35 ...
36 TypeError: Parameter n must be int or passive of cast to int.
37 """
38 try:
39 n = int(n)
40 except (TypeError, ValueError):
41 raise TypeError("Parameter n must be int or passive of cast to int.")
42 if n <= 0:
43 raise ValueError("Parameter n must be greater or equal to one.")
44
45 i = 2
46 ans = 0
47
48 if n == 2:
49 return 2
50
51 while n > 2:
52 while n % i != 0:
53 i += 1
54
55 ans = i
56
57 while n % i == 0:
58 n = n / i
59
60 i += 1
61
62 return int(ans)
63
64
65if __name__ == "__main__":

Calls

no outgoing calls

Tested by

no test coverage detected