我的 python prime finder 中的无限范围? [英] Infinite range in my python prime finder?

查看:28
本文介绍了我的 python prime finder 中的无限范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的 python 质数查找器中获得无限范围!这是我的代码!

导入数学打印欢迎使用 Prime Finder!"option = raw_input("继续(y/n)")如果选项==y":对于范围内的 num(1,(无穷大)):if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):打印编号

我想知道它所说的(无限数)实际上是一个无限数.是否有一些价值或我可以用来找到它的东西?任何帮助将不胜感激!

解决方案

您可以导入 itertools 和使用 count 函数

import itertools对于 itertools.count(1) 中的 num:打印编号

<块引用>

count(1) --> 1 2 3 4 5 ...

count(10) --> 10 11 12 13 14 ...

count(1, 2) --> 1 3 5 7 9 ...

第一个参数是起点.

I am trying to get an infinite range in my python prime number finder! here is my code!

import math
print "Welcome to Prime Finder!"
option = raw_input("continue(y/n)")
 if option == "y":
    for num in range(1,(infinite number)):
        if all(num%i!=0 for i in range(2,int(math.sqrt(num))+1)):
           print num

I am trying to get where it says (infinite number) to actually be an infinite number. Is there some value or something that I can use to find that? Any help would be greatly appreciated!

解决方案

You can import itertools and use count function

import itertools
for num in itertools.count(1):
    print num

count(1) --> 1 2 3 4 5 ...

count(10) --> 10 11 12 13 14 ...

count(1, 2) --> 1 3 5 7 9 ...

The first argument is the starting point.

这篇关于我的 python prime finder 中的无限范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆