Codility经典算法题之三十二:CommonPrimeDivisors

本文介绍了一种算法,用于检查两个整数是否具有完全相同的素数约数。通过求最大公约数并验证每个数是否仅由该公约数的素因数构成来实现。适用于编程竞赛及实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Task description:

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:

class Solution { public int solution(int[] A, int[] 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] = 5

the function should return 1, because only one pair (15, 75) has the same set of prime divisors.

Assume that:

  • Z is an integer within the range [1..6,000];
  • each element of arrays A, B is an integer within the range [1..2,147,483,647].

Complexity:

  • expected worst-case time complexity is O(Z*log(max(A)+max(B))2);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Solution:

判断两个数是否有相同的素数约数。首先求出公约数gcd_val,那么gcd_val里应该包含了common prime divisor,下面分别判断a跟b与gcd_val的公约数是不是有自己的非common prime divisor的prime divisor

def gcd(x, y):
    # Compute the greatest common divisor
    if x%y == 0:
        return y;
    else:
        return gcd(y, x%y)


def hasSamePrimeDivisors(x, y):
    gcd_value = gcd(x, y)   # The gcd contains all
                            # the common prime divisors


    while x != 1:
        x_gcd = gcd(x, gcd_value)
        if x_gcd == 1:
            # x does not contain any more 
            # common prime divisors
            break
        x /= x_gcd
    if x != 1:
        # If x and y have exactly the same common 
        # prime divisors, x must be composed by
        # the prime divisors in gcd_value. So
        # after previous loop, x must be one.
        return False


    while y != 1:
        y_gcd = gcd(y, gcd_value)
        if y_gcd == 1:
            # y does not contain any more 
            # common prime divisors
            break
        y /= y_gcd


    return y == 1


def solution(A, B):
    count = 0
    for x,y in zip(A,B):
        if hasSamePrimeDivisors(x,y):
            count += 1
    return count

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值