-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathCount common factors.py
More file actions
100 lines (41 loc) · 1.09 KB
/
Copy pathCount common factors.py
File metadata and controls
100 lines (41 loc) · 1.09 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
@@ -0,0 +1,99 @@
# Python3 implementation of program
import math
# Map to store the count of each
# prime factor of a
ma = {}
# Function that calculate the count of
# each prime factor of a number
def primeFactorize(a):
sqt = int(math.sqrt(a))
for i in range(2, sqt, 2):
cnt = 0
while (a % i == 0):
cnt += 1
a /= i
ma[i] = cnt
if (a > 1):
ma[a] = 1
# Function to calculate all common
# divisors of two given numbers
# a, b --> input integer numbers
def commDiv(a, b):
# Find count of each prime factor of a
primeFactorize(a)
# stores number of common divisors
res = 1
# Find the count of prime factors
# of b using distinct prime factors of a
for key, value in ma.items():
cnt = 0
while (b % key == 0):
b /= key
cnt += 1
# Prime factor of common divisor
# has minimum cnt of both a and b
res *= (min(cnt, value) + 1)
return res
# Driver code
a = 12
b = 24
print(commDiv(a, b))