【问题标题】:Looking for Clarification on how this "For-Loop" works寻找有关此“For-Loop”如何工作的说明
【发布时间】:2018-09-05 13:22:36
【问题描述】:

我是一个完全的编程初学者,所以请原谅我的幼稚。

我想用 Python 编写一个程序,让我打印给定的N 数量的素数,其中N 由用户输入。我在“for/while”循环上进行了一些搜索并做了一些修改。我运行了一个我在网上看到的程序并对其进行了修改以适应问题。代码如下:

i = 1

print("Hi! Let's print the first N prime numbers.")
nPrimes = int(input("Enter your N: "))

counter =  0

while True:
    c = 0 #another initialization
    for x in range (1, (i + 1)):
        a = i % x # "a" is a new variable that got introduced.
        if a == 0:
            c = c + 1
    if c == 2:
        print(i, end = "  ")
        counter = counter + 1
        if counter > = nPrimes: #if it reaches the number input, the loop will end.
            break
    i = i+1

print(":  Are your", nPrimes, "prime number/s!")
print()
print("Thanks for trying!")

这应该能够打印出用户喜欢的素数数量。这是一个有效的代码,虽然我很难理解它。似乎变量 c 在决定是否打印变量 i(在我们的例子中是该间隔期间假定的素数)方面很重要。

每当我们的变量aa = i % x 中具有0 的余数时,我们就执行c + 1c。然后,如果 c 达到 2,则打印当前变量 i,并且一旦找到并打印素数,变量 c 将自身重新初始化为 0。

这是我可以理解的,但是一旦i 的数字达到 4 及以上,我就会感到困惑。 *当 4 的余数等于 0 的范围内有 2+ 个因数时,程序如何跳过而不打印 4? c == 2 不会为 4 并因此打印 4 吗? *该程序将如何继续到下一个数字 5? (假设变量N 是一个足够大的输入)。

任何澄清将不胜感激。非常感谢!

【问题讨论】:

  • c 很重要,因为每个素数 X 都可以被 2 整除,从 1 到 X,即 1 和 X。基本上从 1 开始 --> X 你将 X 除以这些,如果可整除,即 i%x == 0 然后 c 递增。任何素数都应该有 c = 2,因为 1 并且素数是除数。不打印任何其他具有更大 c 的数字。干杯。
  • 例如,对于数字 4.c 从 0 开始。4%1 = 0 --> c=1 然后 4%2 = 0 --> c=2 然后 4%4 = 0 - -> c=3。已经大于 2。
  • 我看到了@HadiFarah!因此,不能打印 4,因为它的 c 不仅仅是 2。而 5、7、9... 是 c==2。非常感谢!我可能想多了:)

标签: python for-loop


【解决方案1】:

Wikipedia我们知道:

素数(或素数)是大于 1 的自然数,不能通过将两个较小的自然数相乘而形成。

所以要找到素数,就是要找到一个自然数,也就是一个整数,它只能整除除以 1 或它自己。这被称为定义方法来寻找素数。

因此,以下循环遍历从 1 到 i 的所有整数, 它计算整数 i 可以被它们精确除以的次数。

for x in range (1, (i + 1)):
    a = i % x # "a" is a new variable that got introduced.
    if a == 0:
        c = c + 1

然后你判断整数i是否只能整除除以1和它自己。 如果是真的,你得到一个素数; 否则你就继续。

if c == 2:
    print(i, end = "  ")
    counter = counter + 1
    if counter > = nPrimes: #if it reaches the number input, the loop will end.
        break

同时,您可以通过将开头的i = 1 更改为i = 2 并添加if 语句来稍微改进这个素数搜索算法:

# Start from 2 instead of 1
# and end at `i - 1` instead of `i`
for x in range (2, i):
    a = i % x # "a" is a new variable that got introduced.
    if a == 0:
        c = c + 1

    # Abandon the loop
    # because an integer with factors other than 1 and itself
    # is unevitably a composite number, not a prime
    if c > 0:
        break

if c == 0:
    print(i, end = "  ")
    counter = counter + 1
    if counter >= nPrimes: #if it reaches the number input, the loop will end.
        break

这种转变提高了程序的效率,因为您避免了不必要和无意义的工作量。


为了防止while 表达式导致潜在的无限循环, 您应该将while True: 替换为while counter < nPrimes:。而代码原来是这样的:

#if it reaches the number input, the loop will end.
while counter < nPrimes:
    c = 0 #another initialization

    # Start from 2 instead of 1
    # and end at `i - 1` instead of `i`
    for x in range (2, i):
        a = i % x # "a" is a new variable that got introduced.
        if a == 0:
            c = c + 1

        # Abandon the loop
        # because an integer with factors other than 1 and itself
        # is unevitably a composite number, not a prime
        if c > 0:
            break

    if c == 0:
        print(i, end = "  ")
        counter = counter + 1

    i = i + 1

如果您想了解更多关于如何提高程序查找素数的效率,请阅读this code in C language。 :P

【讨论】:

  • 谢谢@KaiserKatze,所以我从你的澄清中看到,即使合数确实达到了 c==2,它也不会止步于此,因为它可以被整除的数字更多,并且因此它不会被打印。我非常感谢你的洞察力。你修改的代码也简单多了!
  • @Sorz 你说得对while,我的意思是,永远不要使用while True:,以防无限循环。因此,我将更新我的答案并稍微修改这个while 循环。
【解决方案2】:

在这种情况下,c 用于计算平均分为 i 的数字的数量。

例如,如果i = 8: 8 可以被 1、2、4 和 8 整除。所以c = 4 因为有 4 个东西可以平分

如果i = 5: 5 可以被 1 和 5 整除。所以c = 2 因为有 2 个数字可以整除

如果i = 4(您似乎很困惑):4 可以被 1、2 和 4 整除。所以c = 3,而不是 2。

【讨论】:

  • 非常感谢!我想我是因为我的过度思考而感到困惑。我也忽略了 c==2 的运算符,并认为它等于 2 或更多。现在我很清楚了:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
相关资源
最近更新 更多