当前位置: 首页 > 知识库问答 >
问题:

寻找关于这个“for-loop”如何工作的澄清

凤安然
2023-03-14

我是一个完整的编程初学者,所以请原谅我的天真。

我想用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!")

如有任何澄清,将不胜感激。太感谢你们了!

共有1个答案

司马念
2023-03-14

从维基百科我们知道:

质数(或素数)是一个大于1的自然数,不能由两个较小的自然数相乘而成。

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

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
# 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
#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

如果您想了解更多关于如何提高程序查找素数的效率的信息,请阅读C语言中的这段代码。:P

 类似资料:
  • 我很难清楚地理解MySQL5.6使用/r/t memcache引入了什么。 按照我的理解,memcache本身本质上是一个巨大的、共享的、驻留内存的哈希表,由服务器memcached管理。特别是,它对持久数据存储一无所知,也不提供这方面的服务。它只知道键和值(像Perl散列)。 我认为MySQL5.6引入的是NoSQL API,mySQL客户端可以通过键而不是通过SELECT语句从mySQL服务器

  • 问题内容: 假设我在数据库中存储了一堆假期。 我需要做的是找出下一个工作日,不包括数据库中定义的星期六和公众假期。 例如。 假设今天是2月15日(星期五),而17日和18日是公众假期(在数据库中定义为日期时间)。因此,现在当我按下显示下一个工作日的按钮时,它将返回2月19日。 哪种方法最有效? 问题答案: 最简单 第1步:从数据库获取假期并将其格式化为您的格式,并将其保留在 第2步:创建一个增加天

  • 我只是在寻找澄清和原因。对于这段代码,为什么在公共测试中将“string”放在name、last和number前面不起作用呢?为什么我必须在构造函数外部用Private进行初始化?

  • 构造循环允许您实现像其他语言中最常见的for循环之类的迭代。 它允许你 为迭代设置变量 指定将有条件地终止迭代的表达式 指定用于在每次迭代中执行某些作业的表达式 指定表达式,以及在退出循环之前执行某些工作的表达式 构造的for循环遵循几种语法 - (loop for loop-variable in <a list> do (action) ) (loop for loop-variable

  • 这是来自SQLZOO.NET的问题

  • 我正在尝试使用OpenNLP。我需要它从新闻网站(例如:techcrunch)获得新的组织(初创公司)。我有一个组织模型,我用它来识别出版物中的组织(en-ner-organization)。我有一个问题: 如果有一个关于新的初创公司的出版物,它是昨天诞生的,openNlp会承认它是组织吗? 据我所知-没有。直到我不在这个新的初创公司训练模特,对吧?