-
Notifications
You must be signed in to change notification settings - Fork 13
/
12.2.swift
50 lines (38 loc) · 971 Bytes
/
12.2.swift
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
public func solution(inout A : [Int], inout _ B : [Int]) -> Int {
// write your code in Swift 2.2
var count = 0
func gcd(a: Int, b: Int) -> Int {
if a % b == 0 {
return b
} else {
return gcd(b, b: a % b)
}
}
func hasSamePrimeDivisors(var a: Int, var b: Int) -> Bool {
let gcdValue = gcd(a, b: b)
while a != 1 {
let aGcd = gcd(a, b: gcdValue)
if aGcd == 1 {
break
}
a /= aGcd
}
if a != 1 {
return false
}
while b != 1 {
let bGcd = gcd(b, b: gcdValue)
if bGcd == 1 {
break
}
b /= bGcd
}
return b == 1
}
for i in 0..<A.count {
if hasSamePrimeDivisors(A[i], b: B[i]) {
count += 1
}
}
return count
}