下面小编给大家整理的Python实现高效求解素数代码实例(共含5篇),欢迎阅读!同时,但愿您也能像本文投稿人“hfkj88”一样,积极向本站投稿分享好文章。
这篇文章主要介绍了Python实现高效求解素数代码实例,本文直接给出代码实例,需要的朋友可以参考下
素数是编程中经常需要用到的,
作为学习Python的示例,下面是一个高效求解一个范围内的素数的程序,不需要使用除法或者求模运算。
#coding:utf-8 #设置python文件的编码为utf-8,这样就可以写入中文注释def primeRange(n): myArray=[1 for x in range(n+1)] ##列表解析,生成长度为(n+1)的列表,每个数值都为1 myArray[0]=0 myArray[1]=0 startPos=2 while startPos <= n: if myArray[startPos]==1:key=2resultPos = startPos * key #可知startPos的整数倍都不是素数,设置startPos的整数倍的位置为0表示非素数while resultPos <= n: myArray[resultPos] =0 key += 1 resultPos = startPos *key startPos += 1 resultList=[] ##将最终的素数保存在resultList列表返回 startPos=0 while startPos <= n: if myArray[startPos] == 1:resultList.append(startPos) startPos += 1 return resultListnumString=raw_input(“Input the Range(>3):”)numInt=int(numString)if numInt <= 3: print “The Number Need to be greater than 3”else: primeResult=primeRange(numInt) print “The Result is:”,primeResult
-11-11SublimeText 2编译python出错的解决方法(The system cannot fin
-02-02使用python将mdb数据库文件导入postgresql数据库示例
2014-02-02python基础教程之popen函数操作其它程序的输入和输出示例
2014-04-04Python中的map、reduce和filter浅析
2014-06-06win7 下搭建sublime的python开发环境的配置方法
-09-09rhythmbox中文名乱码问题解决方法
2014-05-05Python获取远程文件大小的函数代码分享
2014-02-02python类参数self使用示例
2008-09-09Python 可爱的大小写
2014-03-03python基础教程之元组操作使用详解
Python开发的单词频率统计工具wordsworth
ssh批量登录并执行命令的python实现代码
python登录QQ邮箱发信的实现代码
php使用递归与迭代实现快速排序示例
二种python发送邮件实例讲解(python发邮件
python实现数通设备端口监控示例
python使用os模块的os.walk遍历文件夹示例
python使用xauth方式登录饭否网然后发消息
爬山算法简介和Python实现实例
Python程序语言快速上手教程
Python入门教程 超详细1小时学会
python 中文乱码问题深入分析
比较详细Python正则表达式操作指
Python字符串的encode与decode研
Python open读写文件实现脚本
Python enumerate遍历数组示例应
Python 深入理解yield
Python+Django在windows下的开发
python 文件和路径操作函数小结
python 字符串split的用法分享
数据结构就是用来将数据组织在一起的结构,换句话说,数据结构是用来存储一系列关联数据的东西。在Python中有四种内建的数据结构,分别是List、Tuple、Dictionary以及Set。本文将通过实例来介绍这些数据结构的用法。
在学习python的过程中,用来练习代码,并且复习数据结构的
#coding:utf-8#author:Elvis class Stack(object): def __init__(self, size=8): self.stack = [] self.size = size self.top = -1 def is_empty(self): if self.top == -1:return True else:return False def is_full(self): if self.top +1 == self.size:return True else:return False def push(self, data): if self.is_full:raise Exception(‘stackOverFlow‘) else:self.top += 1self.stack.append(data) def stack_pop(self): if self.is_empty():raise Exception(‘stackIsEmpty‘) else:self.top -= 1return self.stack.pop() def stack_top(self): if self.is_empty():raise Exception(‘stackIsEmpty‘) else:return self.stack[self.top] def show(self): print self.stack stack = Stack()stack.push(1)stack.push(2)stack.push(‘a‘)stack.push(‘b‘)stack.push(5)stack.push(6)stack.stack_pop()stack.stack_pop()stack.stack_top()stack.is_empty()stack.is_full()stack.show()
以上所述就是本文给大家分享的全部内容了,希望大家能够喜欢,
这篇文章主要介绍了Python实现线程池代码分享,本文直接给出实例代码,需要的朋友可以参考下
原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码
import threadingimport timeimport signalimport os class task_info(object): def __init__(self): self.func = None self.parm0 = None self.parm1 = None self.parm2 = None class task_list(object): def __init__(self): self.tl = [] self.mutex = threading.Lock() self.sem = threading.Semaphore(0) def append(self, ti): self.mutex.acquire() self.tl.append(ti) self.mutex.release() self.sem.release() def fetch(self): self.sem.acquire() self.mutex.acquire() ti = self.tl.pop(0) self.mutex.release() return ti class thrd(threading.Thread): def __init__(self, tl): threading.Thread.__init__(self) self.tl = tl def run(self): while True:tsk = self.tl.fetch()tsk.func(tsk.parm0, tsk.parm1, tsk.parm2) class thrd_pool(object): def __init__(self, thd_count, tl): self.thds = [] for i in range(thd_count):self.thds.append(thrd(tl)) def run(self): for thd in self.thds:thd.start() def func(parm0=None, parm1=None, parm2=None): print ‘count:%s, thrd_name:%s‘%(str(parm0), threading.currentThread().getName()) def cleanup(signo, stkframe): print (‘Oops! Got signal %s‘, signo)os._exit(0) if __name__ == ‘__main__‘: signal.signal(signal.SIGINT, cleanup) signal.signal(signal.SIGQUIT, cleanup) signal.signal(signal.SIGTERM, cleanup) tl = task_list() tp = thrd_pool(6, tl) tp.run() count = 0 while True: ti = task_info() ti.parm0 = count ti.func = func tl.append(ti) count += 1 time.sleep(2) pass