A prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.
A prime D is called a prime divisor of a positive integer P if there exists a positive integer K such that D * K = P. For example, 2 and 5 are prime divisors of 20.
You are given two positive integers N and M. The goal is to check whether the sets of prime divisors of integers N and M are exactly the same.
For example, given:
- N = 15 and M = 75, the prime divisors are the same: {3, 5};
- N = 10 and M = 30, the prime divisors aren't the same: {2, 5} is not equal to {2, 3, 5};
- N = 9 and M = 5, the prime divisors aren't the same: {3} is not equal to {5}.
Write a function:
def solution(A, B)
that, given two non-empty arrays A and B of Z integers, returns the number of positions K for which the prime divisors of A[K] and B[K] are exactly the same.
For example, given:
A[0] = 15 B[0] = 75 A[1] = 10 B[1] = 30 A[2] = 3 B[2] = 5the function should return 1, because only one pair (15, 75) has the same set of prime divisors.
Write an efficient algorithm for the following assumptions:
- Z is an integer within the range [1..100,000];
- each element of arrays A and B is an integer within the range [1..2,147,483,647].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a > 1:
def solution(A, B):
# write your code in Python 3.6
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd
def solution(A, B):
# write your code in Python 3.6
pass
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
return True
def solution(A, B):
# write your code in Python 3.6
pass
Invalid result type, int expected, <class 'NoneType'> found.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
while b != 1:
div = gcd(a, b)
if div == 1:
return False
b //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
while b != 1:
div = gcd(a, b)
if div == 1:
return False
b //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
while a != 1:
div = gcd(a, b)
if div == 1:
return False
a //= div
while b != 1:
div = gcd(a, b)
if div == 1:
return False
b //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
c = a
while c != 1:
div = gcd(c, b)
if div == 1:
return False
c //= div
c = b
while c != 1:
div = gcd(a, c)
if div == 1:
return False
c //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
c = a
while c != 1:
div = gcd(c, b)
if div == 1:
return False
c //= div
c = b
while c != 1:
div = gcd(a, c)
if div == 1:
return False
c //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def gcd(a, b):
if a < b:
a, b = b, a
if a % b == 0:
return b
return gcd(b, a % b)
def check(a, b):
if a < b:
a, b = b, a
c = a
while c != 1:
div = gcd(c, b)
if div == 1:
return False
c //= div
c = b
while c != 1:
div = gcd(a, c)
if div == 1:
return False
c //= div
return True
def solution(A, B):
# write your code in Python 3.6
return sum(1 for i in range(len(A)) if check(A[i], B[i]))
The solution obtained perfect score.