# 写一个实现迭代器协议的类 Primes 让此类可以生成从b开始的n个素数
# class Primes:
#     def __init__(self, b, n):
#        ....
#     ....
# for x in Primes(10, 4):
#     print(x)  # 11 13 17 19
# 

class Primes(object):
    @staticmethod
    def __isprime(x):
        for i in range(2, x):
            if x % i == 0:
                return False
        return True

    def __init__(self, b, n):
        self.begin = b
        self.count = n
    def __iter__(self):
        self.cur_pos = self.begin  # 设置迭代的起始值
        self.cur_count = 0  # 用于记录已生成几个
        return self

    def __next__(self):
        # 已完成生成,不需要再生成,我停止迭代
        if self.cur_count >= self.count:
            raise StopIteration
        self.cur_count += 1  # 计数加1
        while True:
            if self.__isprime(self.cur_pos):
                v = self.cur_pos
                self.cur_pos += 1
                return v
            self.cur_pos += 1  # 为下一次循环做准备



for x in Primes(10, 4):
    print(x)  # 11 13 17 19

 

相关文章:

  • 2021-07-29
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2022-03-09
  • 2021-11-10
  • 2021-10-25
  • 2021-06-16
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-09
  • 2021-08-14
  • 2022-12-23
  • 2022-01-29
  • 2022-02-01
相关资源
相似解决方案