python - 使用埃拉托色尼筛法的素数生成器
问题描述
此 python 代码使用 Sieve 技术生成素数。我无法理解代码,尤其是yield from
部分。如何跟踪这样的代码或使其更易于理解。这段代码来自 Computerphile Youtube 频道关于 python 惰性的视频。
def nats(n):
yield n
yield from nats(n+1)
def sieve(s):
n = next(s)
yield n
yield from sieve(i for i in s if i%n != 0)
p = sieve(nats(2))
next(p) # generates prime numbers 2, 3, 5, 7, 11, ........
解决方案
推荐阅读
- c# - 关联失败
- c# - 使用asp.net mv在ado.net中选择带有where子句的查询
- facebook - 如何让 Unity FB.AppRequest 到达他们的接收者?
- java - 运行Kafka生产者时没有错误或输出记录
- r - 创建列表时对数据集进行排序并保持顺序
- javascript - Flask Stripe 集成 - JSON 对象必须是 str 而不是字节
- azure - Azure 的 QueueClient.ReceiveMessagesAsync 在没有消息时返回
- mvvm - SwiftUI 使用高频数据更新 UI
- ros - 点云不更新
- jenkins-pipeline - 如何使用 SSO 身份验证部署到 Anypoint Runtime Manager?