本题来自 Project Euler 第10题:https://projecteuler.net/problem=10

# Project Euler: Problem 10: Summation of primes
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
# Find the sum of all the primes below two million.
# Answer: 142913828922 def f(x): #判断 x 是否为素数,返回bool值
if x == 2:
return True
elif x <= 1:
return False
else:
t = False #判断是否能够整除
for i in range(2, int(x**.5)+1):
if x%i == 0:
t = True
break
if t: #若能整除
return False
else:
return True
sum = 0
for i in range(1, 2000000):
if f(i):
sum += i
print(sum)

又是一道求解素数的题目。看来,只要能找到判断素数的最佳方法,就能解决很多问题啊。

本题沿用了之前的素数判断函数。不知道有没有优化的版本,能大幅压缩计算时间啊……

Python练习题 038:Project Euler 010:两百万以内所有素数之和的更多相关文章

  1. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

  2. Python练习题 047:Project Euler 020:阶乘结果各数字之和

    本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...

  3. Python练习题 044:Project Euler 016:乘方结果各个数值之和

    本题来自 Project Euler 第16题:https://projecteuler.net/problem=16 ''' Project Euler 16: Power digit sum 2* ...

  4. Python练习题 035:Project Euler 007:第10001个素数

    本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...

  5. Python3练习题 035:Project Euler 007:第10001个素数

    import time def f(x): #判断 x 是否为素数,返回bool值 if x == 2: return True elif x <= 1: return False else: ...

  6. Python练习题 028:求3*3矩阵对角线数字之和

    [Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...

  7. Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4 # Project Euler: Problem 4: Largest palind ...

  8. Python练习题 033:Project Euler 005:最小公倍数

    本题来自 Project Euler 第5题:https://projecteuler.net/problem=5 # Project Euler: Problem 5: Smallest multi ...

  9. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

随机推荐

  1. day40:python操作mysql:pymysql模块&SQL注入攻击

    目录 part1:用python连接mysql 1.用python连接mysql的基本语法 2.用python 创建&删除表 3.用python操作事务处理 part2:sql注入攻击 1.s ...

  2. 焦大:seo思维进化论(下)

    http://www.wocaoseo.com/thread-50-1-1.html 很多东西在不同地方其所有的价值和意义是不一样的,seo亦是如此.在seo操作中我觉得最核心的就是检索价值观和用户需 ...

  3. C语言如何动态分配二维数组

    C语言如何动态分配二维数组(转载) 原文链接:https://www.cnblogs.com/0xWitch/p/9314621.html 使用malloc().free()函数进行动态分配,这两个函 ...

  4. 基于laravel的有偿开源流程引擎

    系统主要文档已经编写完成,具体请前往查看[系统文档](https://www.kancloud.cn/lijianlin/jishullin_workflow_engine/1894424 " ...

  5. 微信小程序|小游戏

    [官]小游戏开发 https://developers.weixin.qq.com/minigame/dev/index.html 官网 https://mp.weixin.qq.com 做了4个微信 ...

  6. 【干货满满】1.5w字初中级前端面试复习总结

    前言 金九银十,又是一波跑路.趁着有空把前端基础和面试相关的知识点都系统的学习一遍,参考一些权威的书籍和优秀的文章,最后加上自己的一些理解,总结出来这篇文章.适合复习和准备面试的同学,其中的知识点包括 ...

  7. Oracle 回滚段undo

    Undo的作用 数据的回滚 一致性读 表的闪回(事务,查询的闪回..) 失败会话的恢复 回滚rollback操作 SQL> archive log list; ORA-01031: 权限不足 S ...

  8. 极简显示sessionid的jsp程序 war下载

    下载地址:https://files.cnblogs.com/files/xiandedanteng/simpleJspSessionId20200103.zip 解压后得到myweb.war就是可以 ...

  9. selenium+python对表格数据的操作

    一.直接获取整个表格数据,包含表头 def table_info(self): tr_data=[] table_data=[] css='id=>useradmin'#根据表格id找到表格 s ...

  10. 如何入门Pytorch之四:搭建神经网络训练MNIST

    上一节我们学习了Pytorch优化网络的基本方法,本节我们将以MNIST数据集为例,通过搭建一个完整的神经网络,来加深对Pytorch的理解. 一.数据集 MNIST是一个非常经典的数据集,下载链接: ...