【每日一题20220614】互为质数

:mage:‍ 给定两个位于2到99之间到数字,如果它们公因子只有数字1,则称它们互为质数。请编写一个函数,判断给定的两个数字是否互为质数,如果是返回True,否则返回False。

【示例】
输入:20, 27
输出:True
解释:20的因子有1, 2, 4, 5, 10, 20;27的因子有1, 3, 9, 27。所以公因子是1,互为质数。

题目难度:简单
题目来源:codewars-Coprime Validator

def solution(n1: int, n2: int) -> bool:
    # your code here

assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n1: int, n2: int) -> bool:
    # your code here
    lis1 = [i for i in range(1, n1 + 1) if n1 % i == 0]
    lis2 = [i for i in range(1, n2 + 1) if n2 % i == 0]
    return True if set(lis1).intersection(lis2) == {1} else False


assert solution(20, 27) is True
assert solution(12, 39) is False

2 个赞
def solution(n1: int, n2: int) -> bool:
    # your code here
    max_num = max(n1, n2)
    index_num = 2
    while index_num <= max_num:
        if n1 % index_num == 0 and n2 % index_num == 0:
            return False
        index_num += 1
    return True

assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n1: int, n2: int) -> bool:
    if n1 > n2:
        n1, n2 = n2, n1
    for i in range(2, n1 + 1):
        if n1 % i == 0 and n2 % i == 0:
            return False
    return True
def solution(n1: int, n2: int):
    flag = True
    for i in range(2, n1 + 1):
        if n1 % i == 0:
            if n2 % i == 0:
                flag = False
                break
    return flag

def solution(n1: int, n2: int) -> bool:
    def factor_list(num: int) -> set:
        s = set()
        for i in range(1, int(num ** 0.5) + 1):
            if num % i == 0:
                j = num / i
                s.update([i, j])
        return s

    new_set = factor_list(n1) & factor_list(n2)
    if len(new_set) > 1:
        return False
    return True


assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n1:int,n2:int) -> bool:
    while (n1%n2)!=0:
        r = n1%n2
        n1 = n2
        n2 = r
    return True if n2==1 else False

assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n1: int, n2: int)-> bool:
    rest1 = [i for i in range(1, n1) if n1 % i == 0]
    rest2 = [i for i in range(1, n2) if n2 % i == 0]

    if (set(rest1) & set(rest2) ) != {1}:
        return False

    return True

assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n1: int, n2: int) -> bool:
    # your code here
    tmp = n1 if n1 > n2 else n2
    for i in range(2, tmp+1):
        if n1 % i == 0 and n2 % i == 0:
            return False
    return True


assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False
def solution(n,m):
    max_v=max(m,n)
    min_v=min(m,n)
    #欧几里得算法(辗转相除法)
    #以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数
    while max_v % min_v != 0:
        max_v,min_v = min_v,max_v%min_v
    return min_v==1
    #解法二
    for i in range(2,n+1):
        if n % i ==0 and m % i == 0:
            return False
    return True
def solution(n1: int, n2: int) -> bool:
    n1_list=[i for i in range(1,n1+1) if n1%i==0]
    n2_list=[i for i in range(1,n2+1) if n2%i==0]
    return True if 1 in n1_list and 1 in n2_list and len(set(n1_list+n2_list))==len(n1_list)+len(n2_list)-1 else False

assert solution(20, 27) is True
assert solution(12, 39) is False
assert solution(15, 98) is True
assert solution(18, 98) is False