赞
踩
- primes=[2]
- for num in range(3, 1000 + 1, 2):
- is_prime = True
- for i in range(2, int(num ** 0.5) + 1):
- if num % i == 0:
- is_prime = False
- break
- if is_prime:
- primes.append(num)
-
- print(primes)
每行输出10个:
- prime_number=[]
-
- i=0
- for num in range(1,1000+1):
- divisors = []
- for n in range(1,num+1):
- divisor=num%n
- if divisor==0:
- divisors.append(divisor)
- if len(divisors)==2:
- prime_number.append(num)
- i += 1
- if i%10==0 and prime_number:
- print(prime_number)
- prime_number=[]
- print(prime_number)
- i = 1
- print(2, end='\t\t')
- for num in range(1, 1000+1, 2):#素数的话必然不是偶数,偶数是能够被2所整除的整数
- #这里的除数最大是判断的被除数的算数平方根,大于这个算数平方根的除数就毫无意义,因为被除数除以大于算数平方根的商必然在1到算数平方根之间
- for n in range(2, int(pow(num, .5))+1):
- if num % n == 0:
- break
- else:
- if i % 10 == 0 and num!=1:
- print(num, end='\n')
- elif num!=1:
- print(num, end='\t\t')
- i += 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。