计算质数函数

import pyprind
from matplotlib import pyplot as plt
import numpy as np


def prime(n_iter: int = 10, min_num: int = None, max_num: int = None) -> object:
    """


    :rtype: list
    """
    pbar = pyprind.ProgBar(n_iter)
    prime = []
    if min_num:
        r = min_num-1
        while True:
            r += 1
            decide = False
            for i in range(1, r+1):
                if i != 1 and i != r:
                    #  print('i=',i)
                    #  print('r=',r)
                    for j in range(i, r):
                        #  print('j=',j)
                        if i*j == r:
                            decide = True
                            break
                    if decide:
                        break
                #  print('---------')
            if not decide:
                prime.append(r)
                pbar.update()
                if len(prime) == n_iter:
                    return prime
    if max_num:
        r = max_num+1
        while True:
            r -= 1
            decide = False
            for i in range(1, r+1)[::-1]:
                if i != 1 and i != r:
                    # print('i=', i)
                    # print('r=', r)
                    for j in range(2, r):
                        # print('j=', j)
                        if i*j == r:
                            decide = True
                            break
                    if decide:
                        break
                elif r == 2:
                    prime.append(1)
                    return prime
                # print('--------')
            if not decide:
                prime.append(r)
                pbar.update()
                if len(prime) == n_iter:
                    return prime

比如prime(30, max_num=1061)

 

你可能感兴趣的:(python,python,开发语言)