python输出100以内的质数与合数实例代码

具体代码如下所述:

__author__ = 'Yue Qingxuan'
# -*- coding: utf-8 -*-
#求质数
p=[2]
for i in range(2,101):
  for temp in range(2,i):
    if i%temp==0:
      break
      print('i=',i,'temp=',temp)
    elif temp==i-1:
      p.append(i)
print('\n以下打印质数:')
print(p)
#求合数
list=[]
for i in range (4,100):
  for j in range(2,i):
    if(i%j==0):
      list.append(i)
      break
  #else:
    #break
    #list.append(i)
print('\n\n以下打印合数:')
print(list)

本程序用python3运行,以下是程序输出结果:

以下打印质数:

[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]

以下打印合数:

[4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77, 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99]

总结

以上所述是小编给大家介绍的python输出100以内的质数与合数实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关推荐