เป็น python 2.6.1 นะครับ
__author__="nuboat"
__date__ ="$22 03 2552 20:30:00$"
list_prime = [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]
def findfactor(data, list_result):
if data < 2:
return None
for i in list_prime:
if (data % i) == 0:
list_result.append(i)
findfactor( data/i, list_result)
break;
if __name__ == "__main__":
for x in xrange(100):
list_result = []
findfactor(x, list_result)
if len(list_result) != 0:
print "Factor of " + str(x) + " is " + str(set(list_result))
else:
print "Factor of " + str(x) + " is " + str([].append(x))
โค้ดรอบสองแก้ส่วนไร้สาระออกเฉยๆ
list_prime = [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]
def findfactor(data, list_result):
for i in [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]:
if (data % i) == 0:
list_result.append(i)
findfactor( data/i, list_result)
if __name__ == "__main__":
for x in xrange(2,100):
list_result = []
findfactor(x, list_result)
print len(list_result)!=0 and "Factor of " + str(x) + " is " + str(set(list_result)) or "Factor of " + str(x) + " is " + str([].append(x));
Factor of 0 is None
Factor of 1 is None
Factor of 2 is set([2])
Factor of 3 is set([3])
Factor of 4 is set([2])
Factor of 5 is set([5])
Factor of 6 is set([2, 3])
Factor of 7 is set([7])
Factor of 8 is set([2])
Factor of 9 is set([3])
Factor of 10 is set([2, 5])
Factor of 11 is set([11])
Factor of 12 is set([2, 3])
Factor of 13 is set([13])
Factor of 14 is set([2, 7])
Factor of 15 is set([3, 5])
Factor of 16 is set([2])
Factor of 17 is set([17])
Factor of 18 is set([2, 3])
Factor of 19 is set([19])
Factor of 20 is set([2, 5])
Factor of 21 is set([3, 7])
Factor of 22 is set([2, 11])
Factor of 23 is set([23])
Factor of 24 is set([2, 3])
Factor of 25 is set([5])
Factor of 26 is set([2, 13])
Factor of 27 is set([3])
Factor of 28 is set([2, 7])
Factor of 29 is set([29])
Factor of 30 is set([2, 3, 5])
Factor of 31 is set([31])
Factor of 32 is set([2])
Factor of 33 is set([11, 3])
Factor of 34 is set([17, 2])
Factor of 35 is set([5, 7])
Factor of 36 is set([2, 3])
Factor of 37 is set([37])
Factor of 38 is set([2, 19])
Factor of 39 is set([3, 13])
Factor of 40 is set([2, 5])
Factor of 41 is set([41])
Factor of 42 is set([2, 3, 7])
Factor of 43 is set([43])
Factor of 44 is set([2, 11])
Factor of 45 is set([3, 5])
Factor of 46 is set([2, 23])
Factor of 47 is set([47])
Factor of 48 is set([2, 3])
Factor of 49 is set([7])
Factor of 50 is set([2, 5])
Factor of 51 is set([17, 3])
Factor of 52 is set([2, 13])
Factor of 53 is set([53])
Factor of 54 is set([2, 3])
Factor of 55 is set([11, 5])
Factor of 56 is set([2, 7])
Factor of 57 is set([19, 3])
Factor of 58 is set([2, 29])
Factor of 59 is set([59])
Factor of 60 is set([2, 3, 5])
Factor of 61 is set([61])
Factor of 62 is set([2, 31])
Factor of 63 is set([3, 7])
Factor of 64 is set([2])
Factor of 65 is set([5, 13])
Factor of 66 is set([11, 2, 3])
Factor of 67 is set([67])
Factor of 68 is set([17, 2])
Factor of 69 is set([3, 23])
Factor of 70 is set([2, 5, 7])
Factor of 71 is set([71])
Factor of 72 is set([2, 3])
Factor of 73 is set([73])
Factor of 74 is set([2, 37])
Factor of 75 is set([3, 5])
Factor of 76 is set([2, 19])
Factor of 77 is set([11, 7])
Factor of 78 is set([2, 3, 13])
Factor of 79 is set([79])
Factor of 80 is set([2, 5])
Factor of 81 is set([3])
Factor of 82 is set([41, 2])
Factor of 83 is set([83])
Factor of 84 is set([2, 3, 7])
Factor of 85 is set([17, 5])
Factor of 86 is set([2, 43])
Factor of 87 is set([3, 29])
Factor of 88 is set([2, 11])
Factor of 89 is set([89])
Factor of 90 is set([2, 3, 5])
Factor of 91 is set([13, 7])
Factor of 92 is set([2, 23])
Factor of 93 is set([3, 31])
Factor of 94 is set([2, 47])
Factor of 95 is set([19, 5])
Factor of 96 is set([2, 3])
Factor of 97 is set([97])
Factor of 98 is set([2, 7])
Factor of 99 is set([11, 3])
This post has been edited by aiboat: 24 March 2009 - 09:25 PM