编写判断素数的函数。在主调函数中输出1-100之间的素数。

#-*- coding:UTF-8 -*-
__author__ = "zhangguodong"
__time__ ="2017.09.29"

import math
def allPrime(maxNumber):
    prime = []
    for n in range(2, maxNumber):
        if is_prime(n):
            prime.append(n)
    prime = map(str, prime)
    primes = ",".join(prime)
    print "The primes between 1 to100 are: "+primes

def is_prime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n%i == 0:
            return False
    return True
allPrime(100)

运行结果:

The primes between 1 to100 are: 	2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97


你可能感兴趣的:(python学习)