简体   繁体   English

为什么这个问题的 for 循环解决方案比我的 while 循环解决方案快得多? 我完全被难住了

[英]Why is this for loop solution to this problem sooooo much faster than my while loop solution? I'm completely stumped

I'm taking a Python course through Udemy.我正在通过 Udemy 参加 Python 课程。 I have a fair amount of Java experience (for Android) but it's been a while so maybe I'm missing something obvious but I can't find it.我有相当多的 Java 经验(适用于 Android),但已经有一段时间了,所以也许我遗漏了一些明显的东西,但我找不到。

The assignment was to create a function that takes an int then returns True or False based on whether or not you fed it a prime number.任务是创建一个 function ,它接受一个 int 然后根据你是否给它一个质数返回 True 或 False。 Then use that function to find the first 5 primes over 1,000,000.然后使用 function 找到超过 1,000,000 的前 5 个素数。

My solution worked correctly but took a full 3438ms to find the primes.我的解决方案工作正常,但花了整整 3438 毫秒才能找到素数。 The highest of the five is 1,000,081... so three seconds to loop 81 times?五个中最高的是 1,000,081... 所以三秒循环 81 次? I ran and timed the instructors solution and his did it in 140ms.我运行并计时了讲师的解决方案,他在 140 毫秒内完成了。

The main difference between his and mine is that I'm using a while loop and he's using a for loop with a set max range.他和我的主要区别在于我使用的是 while 循环,而他使用的是设置最大范围的 for 循环。 But that can't be it, what am I missing here?但这不可能,我在这里错过了什么?

My code:我的代码:

def prime_checker(number):

    local_list = list()

    for count in range(number):
        if number % (count + 1) == 0 and count + 1 != number:
            local_list.append(count)

    if len(local_list) == 1:
        return True
    else:
        return False

current_count = 999999
prime_list = list()


while len(prime_list) < 5:

    current_count += 1
    print('current_count -> ', current_count)

    if prime_checker(current_count):
        prime_list.append(current_count)
    else:
        continue

print(prime_list)

The instructor's Solution:导师解决方案:

def is_prime(n):
    prime = True
    i = 1
    while i < n // 2:
        i = i + 1
        if n % i == 0:
            prime = False
            break
    return prime

primes = []
for n in range(1_000_000, 100_000_000):
   if is_prime(n):
       primes.append(n)
       if len(primes) == 5:
           break

print(primes)

The main performance difference comes from your prime_checker logic.主要的性能差异来自您的 prime_checker 逻辑。 Your instructor's is_prime function stops its loop as soon as it finds a factor for the given number.您的讲师的 is_prime function 一旦找到给定数字的因子就会停止其循环。 Your prime_checker builds a complete list of factors and only checks the count onces it has them all.您的 prime_checker 构建了一个完整的因素列表,并且仅在所有因素都检查一次时才检查计数。

For example, is_prime(1_000_000) will exit after performing one iteration returning False upon finding that 1_000_000 is divisible by 2. prime_checker(1_000_000) will divide the number 999998 times to build the list and find that there are 47 divisors.例如,is_prime(1_000_000) 将在执行一次迭代后退出,当发现 1_000_000 可被 2 整除时返回 False。prime_checker(1_000_000) 将数字 999998 次以构建列表并发现有 47 个除数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM