【问题标题】:how can i improve my code on euler problem 7我如何改进我在欧拉问题 7 上的代码
【发布时间】:2019-05-11 20:31:40
【问题描述】:

我已经开始学习python,在做了一些基础之后我开始做欧拉问题。我能够做到 7,但编译需要很长时间。有人可以帮我吗?

这是我写的唯一代码

def prime(n):
    count = 0
    if n <= 1:
        print("Number is neither prime nor composite")
    if n == 2:
        print("Number is prime")
    if n > 2:
        for i in range(2, n//2 + 1):
            if n % i == 0:
                count += 1
            else:
                count += 0
    if count == 0:
        return True
    else: 
        return False


b = 10001
a = []
i = 2
while len(a) < b:
    if prime(i):
        a.append(i)
        i += 1
    else:
        i += 1
print(a[-1])

【问题讨论】:

    标签: python python-3.x optimization primes


    【解决方案1】:

    没有必要找出一个数的所有因数。一旦你找到一个因数,这个数显然不是质数,你可以立即返回False

    编辑:
    正如 Federico Domeniconi 在 cmets 中提到的那样,也不需要迭代多达一半的 n。迭代到它的平方根就足够了:

    if n > 2:
        for i in range(2, int(math.sqrt(n)) + 1):
            if n % i == 0:
                return False
    
        # No factors found, n is a prime:
        return True
    

    【讨论】:

    • 你可以使用range(2, int(math.sqrt(n)) + 1)来改进。
    • @FedericoDomeniconi True 'dat
    • 不应该是int(math.sqrt(n)) + 1
    • @FedericoDomeniconi arg,是的(已编辑)。 range 的第二个参数是独占的。
    • 并跳过偶数,首先检查它是否可被 2 整除。将工作量减少 1/2 以获得素数。
    【解决方案2】:

    如果你真的想简化这种类型的解决方案:

    def is_prime(n):
        factors = range(2, int(math.sqrt(n)) + 1)
        return (n > 1 and all(n % f for f in factors))
    

    我可能曾经知道并忘记的一个意想不到的琐事项目。为什么函数会为n = 2返回True

    factors # range(2, 1 + 1)
            # range(2, 2)
            # Which is an empty range.
            # Or in more concrete form: an empty list.
    
    n > 1   # True
    all([]) # Hmmmmm?
            #
            # Are all of the items in an empty collection true?
            #
            # If every tree falls in the forest at once, but no one hears them,
            # does it make a sonic boom?
            #
            # Contrary to Billy Preston -- and microeconomists world wide --
            # can you really get something from nothing?
            #
            # Python says YES. I'm sure smart mathematics do too, but
            # I would be curious to hear the reasoning.
    

    我猜 StackOverflow 对我的问题有 answer。对于这种情况,逻辑学家有一个name:“vacuous truth 是断言空集的所有成员都具有特定属性的陈述。”

    【讨论】:

    • 那是因为forall x in S. P(x) === forall x in S. not( not( P(x) )) === not( exists x in S. not(P(x)) ) 并且在 S 中肯定没有这样的元素 not(P(x)) 是这种情况,因为 S 中根本没有元素 - 所以我们最终得到not( False )True.
    【解决方案3】:

    如果您正在解决 Project Euler 问题,那么拥有自己的Sieve of Eratosthenes 是个好主意。这可以非常快速地计算素数,比您当前使用的方法尝试分解要快得多。您会发现它对他们的许多问题很有用。

    欧拉 7 的解则变为:

    1. 使用Prime Number Theorem估计第10,001个素数的大小。

    2. 从第 1 步开始,将 Eratosthenes 的筛子运行到极限,为了安全起见,稍微超出一点。

    3. 通过筛子的输出数来找出第​​ 10,001 个素数。

    【讨论】:

      猜你喜欢
      • 2017-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多