-
Notifications
You must be signed in to change notification settings - Fork 0
/
first.py
59 lines (52 loc) · 893 Bytes
/
first.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from operator import floordiv, mod
def divide_exact(n,d=10):
"""Return the quotient and reminder of dividing N by D
>>> q, r = divide_exact(2013, 10)
>>> q
201
>>> r
3
"""
return floordiv(n,d), mod(n,d)
def absolute_value(x):
"""Return the absolute value of x."""
if x < 0:
return -x
elif x==0:
return 0
else:
return x
def prime_factors(n):
"""Print the prime factors of n in none decreasing order
>>> prime_factors(8)
2
2
2
>>> prime_factors(9)
3
3
>>> prime_factors(16)
2
5
>>> prime_factors(11)
11
>>> prime_factors(12)
2
2
3
>>> prime_factors(858)
2
3
11
13
"""
while n > 1:
k = smallest_prime_factor(n)
n = n // k
print(k)
def smallest_prime_factor(n):
"""Return the smallest k > i that evenly divides n."""
k = 2
while n % k != 0:
k = k + 1
return k