forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
four-divisors.py
62 lines (56 loc) · 1.62 KB
/
four-divisors.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
60
61
62
# Time: O(n * sqrt(n))
# Space: O(1)
class Solution(object):
def sumFourDivisors(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
result = 0
for num in nums:
facs, i = [], 1
while i*i <= num:
if num % i:
i+= 1
continue
facs.append(i)
if i != num//i:
facs.append(num//i)
if len(facs) > 4:
break
i += 1
if len(facs) == 4:
result += sum(facs)
return result
# Time: O(n * sqrt(n))
# Space: O(sqrt(n))
import itertools
class Solution2(object):
def sumFourDivisors(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
def factorize(x):
result = []
d = 2
while d*d <= x:
e = 0
while x%d == 0:
x //= d
e += 1
if e:
result.append([d, e])
d += 1 if d == 2 else 2
if x > 1:
result.append([x, 1])
return result
result = 0
for facs in itertools.imap(factorize, nums):
if len(facs) == 1 and facs[0][1] == 3:
p = facs[0][0]
result += (p**4-1)//(p-1) # p^0 + p^1 +p^2 +p^3
elif len(facs) == 2 and facs[0][1] == facs[1][1] == 1:
p, q = facs[0][0], facs[1][0]
result += (1 + p) * (1 + q)
return result