code.club

標題: 得出某數之內有哪些是質數的函式 [打印本頁]

作者: CDC    時間: 2018-1-8 17:36
標題: 得出某數之內有哪些是質數的函式
本帖最後由 CDC 於 2018-1-8 17:37 編輯

def how_many_prime(n):
    total = 0
    for i in range(n+1):
        is_prime = True
        if i>=2:
            for j in range(i):
                if j>1 and i%j==0:
                    is_prime = False
                    break
            if is_prime == True:
                total += 1
                print(i," is a prime.")
    print("There are",total,"prime numbers in",n,".")
作者: CDC    時間: 2020-11-25 18:28
def prime_check(num):
    total = 0
    for i in range(2,num+1):
        is_prime = True
        for j in range(2,i//2+1):
            if i%j==0:
                is_prime=False
                break
        if is_prime==True:
            total += 1
            print(i,'is a prime.',end=' ')
    print("There are",total,"prime numbers.")




歡迎光臨 code.club (http://code.club/) Powered by Discuz! X3.2