File tree 1 file changed +63
-0
lines changed
1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Problem:
3
+ The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor
4
+ of a given number N?
5
+
6
+ e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
7
+ """
8
+
9
+
10
+ def solution (n : int ) -> int :
11
+ """Returns the largest prime factor of a given number n.
12
+
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
+ i = 2
45
+ ans = 0
46
+ if n == 2 :
47
+ return 2
48
+ while n > 2 :
49
+ while n % i != 0 :
50
+ i += 1
51
+ ans = i
52
+ while n % i == 0 :
53
+ n = n / i
54
+ i += 1
55
+
56
+ return int (ans )
57
+
58
+
59
+ if __name__ == "__main__" :
60
+ # print(solution(int(input().strip())))
61
+ import doctest
62
+
63
+ doctest .testmod ()
You can’t perform that action at this time.
0 commit comments