开发者社区> 问答> 正文

返回x的除数的最小质数

以下代码包含一个可能触发无限循环的错误,我无法弄清楚如何运行第二个print语句,我敢肯定修复它很简单,但是看不到它。

def smallest_prime_factor(x):
    """Returns the smallest prime number that is a divisor of x"""
    # Start checking with 2, then move up one by one
    n = 2
    while n <= x:
        if x % n == 0:
            x += 1
            return n

print(smallest_prime_factor(12)) # should be 2
print(smallest_prime_factor(15)) # should be 3

问题来源:stackoverflow

展开
收起
is大龙 2020-03-23 17:16:43 471 0
1 条回答
写回答
取消 提交回答
  • 您陷入无限循环,因为如果不满足返回条件,则不更改n的值,因为您可以看到返回条件仅在您的数字x是2的倍数时得到满足改变 :

    if x % n == 0:
        x += 1
    

    与:

    while n <= x:
        if x % n == 0:
            return x
        n += 1
    

    为了优化代码,您可以搜索一个以质数n来除以int(math.sqrt(x)+ 1)x

    import math
    
    def smallest_prime_factor(x):
        """Returns the smallest prime number that is a divisor of x"""
        # Start checking with 2, then move up one by one
        n = 2
        max_n = int(math.sqrt(x) + 1)
        while n < max_n:
            if x % n == 0:
                return n
            n += 1
    
        return x
    

    甚至更好的是,您可以使用Eratosthenes筛子快速生成质数并针对x进行测试:

    # Sieve of Eratosthenes
    # Code by David Eppstein, UC Irvine, 28 Feb 2002
    # http://code.activestate.com/recipes/117119/
    
    def gen_primes(y):
        """ Generate an infinite sequence of prime numbers.
        """
        # Maps composites to primes witnessing their compositeness.
        # This is memory efficient, as the sieve is not "run forward"
        # indefinitely, but only as long as required by the current
        # number being tested.
        #
        D = {}
    
        # The running integer that's checked for primeness
        q = 2
    
        while q < y:
            if q not in D:
                # q is a new prime.
                # Yield it and mark its first multiple that isn't
                # already marked in previous iterations
                # 
                yield q
                D[q * q] = [q]
            else:
                # q is composite. D[q] is the list of primes that
                # divide it. Since we've reached q, we no longer
                # need it in the map, but we'll mark the next 
                # multiples of its witnesses to prepare for larger
                # numbers
                # 
                for p in D[q]:
                    D.setdefault(p + q, []).append(p)
                del D[q]
    
    
    def smallest_prime_factor(x):
        """Returns the smallest prime number that is a divisor of x"""
        # Start checking with 2, then move up one by one
    
        return next((i for i in gen_primes(int(math.sqrt(x) + 1)) if x % i == 0), x)
    

    回答来源:stackoverflow

    2020-03-23 17:16:53
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载