- 论坛徽章:
- 0
|
记得在坛子里有过一个质数求解的贴子
http://bbs.chinaunix.net/forum/v ... der=asc&start=0
用SHELL我还搞不定,用python写了个,不过只是判断一个数是否为质数;哈哈 有点不对题 :em12: ;可能性能不怎么样,我也刚学,又不知发那好就发这吧,希望有点用处,还请版主别册,放几天.实在是找不到什么有人气的PYTHON论坛,或者我懒吧 :em12: :em12: :em12:
- # Name prime-judge
- import math
- number=raw_input("Enter a integer: ")
- integer=int(number)
- string=number[ len(number) - 1 ]
- integer1=int(string)
- float=math.sqrt(integer)
- tmp=math.floor(float)
- isodd=0
- def f2d(x):
- return "%d" % x
- def printMate():
- print "This is a mate number"
- def printOdd():
- print "This is a odd number"
- def printPrime():
- print "This is a prime number"
- M=int( f2d(tmp) )
- if integer1 % 2 ==0:
- printMate()
- elif integer % 3 == 0 or integer % 5 == 0 or integer % 7 ==0:
- printOdd()
- elif float -tmp == 0:
- printOdd()
- elif M % 2 == 0:
- odd=range(3,M,2)
- for i in list(odd):
- if integer % i == 0:
- isodd +=1
- print i,"x",integer / i
- printOdd()
- break
- if isodd == 0:
- printPrime()
- else:
- odd=range(3,M+1,2)
- for i in list(odd):
- if integer % i == 0:
- isodd +=1
- print i,"x",integer / i
- printOdd()
- break
- if isodd == 0:
- printPrime()
复制代码
有兴趣的朋友可以交流下就太好了.QQ:358314689 注明python |
|