Reference:

[1] Python all() - Python Standard Library

[2] Python any() - Python Standard Library

all() and any() 函数主要用于需要判断某个数组是不是都满足了某种条件,设置一个跟数组一样的 bool 数组,判断 bool 数组是否都为 True,或者有没有 True 等等。

Q:

Insert your code into consecutive_primes.py so as to find all sequences of 6 consecutive prime 5-digit numbers, say (a,b,c,d,e,f), with b=a+2, c=b+4, d=c+6, e=d+8, and f=e+10. So a, b, c, d, e and f are all 5-digit prime numbers and no number between a and b, between b and c, between c and d, between d and e, and between e and f is prime.

If you are stuck, but only when you are stuck, then use consecutive_primes_scaffold.py.

A: by McDelfino,在这个程序中使用了 all() 函数用来判断是否对应的数为质数,如果都返回 True 才会通过。

from math import sqrt

def is_prime(n):
if n%2 == 0: return False
for i in range(3,round(sqrt(n))+1, 2):
if n%i == 0:
return False
return True print('The solutions are:\n')
# The list of all even i's such that a + i is one of a, b, c, d, e, f.
good_leaps = tuple(sum(range(0, k, 2)) for k in range(2, 13, 2)) # Write a loop that generates all odd numbers a between 10_000 and 99_999 and tests whether
# for all i = 0, 2, 4, ..., 30, i is in good_leaps iff a + i is prime.
for a in range(10_001, 100_000-5, 2):
if all([is_prime(a+i) for i in good_leaps]):
tmp = [a+i for i in good_leaps]
count = 0
for j in range(tmp[1]+2, tmp[5], 2):
if is_prime(j): count+=1
if count == 3:
print(*[a+i for i in good_leaps])
The solutions are:

13901 13903 13907 13913 13921 13931
21557 21559 21563 21569 21577 21587
28277 28279 28283 28289 28297 28307
55661 55663 55667 55673 55681 55691
68897 68899 68903 68909 68917 68927

【378】python any() and all()的更多相关文章

  1. 【Lab】Python改bat文件

    [Lab]Python改bat文件 给出一个特定的树形结构,每一层的数字依次递增后,按照从上到下,同时从左到右这样的顺序生成.这么说还是不太明白,比如下面这个简单的树形结构. 按照顺序应该写成这样[3 ...

  2. 【转】Python函数默认参数陷阱

    [转]Python函数默认参数陷阱 阅读目录 可变对象与不可变对象 函数默认参数陷阱 默认参数原理 避免 修饰器方法 扩展 参考 请看如下一段程序: def extend_list(v, li=[]) ...

  3. 【转】Python多进程编程

    [转]Python多进程编程 序. multiprocessingpython中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程.Pytho ...

  4. 【导航】Python相关

    [博客导航] Python相关导航 [索引]Python常用资源(从新手到大牛) [任务]Python语言程序设计.MOOC学习 [笔记]Python集成开发环境——PyCharm 2018.3下载. ...

  5. 【转】Python模块学习 - fnmatch & glob

    [转]Python模块学习 - fnmatch & glob 介绍 fnmatch 和 glob 模块都是用来做字符串匹配文件名的标准库. fnmatch模块 大部分情况下使用字符串匹配查找特 ...

  6. 【转】Python之mmap内存映射模块(大文本处理)说明

    [转]Python之mmap内存映射模块(大文本处理)说明 背景: 通常在UNIX下面处理文本文件的方法是sed.awk等shell命令,对于处理大文件受CPU,IO等因素影响,对服务器也有一定的压力 ...

  7. 【转】python之模块array

    [转]python之模块array >>> import array#定义了一种序列数据结构 >>> help(array) #创建数组,相当于初始化一个数组,如: ...

  8. 【转】python 退出程序的方式

    [转]python 退出程序的方式 python程序退出方式[sys.exit() os._exit() os.kill() os.popen(...)] 知乎说明 http://www.zhihu. ...

  9. 【转】python 面向对象(进阶篇)

    [转]python 面向对象(进阶篇) 上一篇<Python 面向对象(初级篇)>文章介绍了面向对象基本知识: 面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 ...

随机推荐

  1. centos 7 修改host文件

    centos7与之前的版本都不一样,修改主机名在/ect/hostname 和/ect/hosts 这两个文件控制 首先修改/etc/hostname vi /etc/hostname 打开之后的内容 ...

  2. 【Redis】编译错误zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory

    [Redis]编译错误zmalloc.h:50:31: fatal error: jemalloc/jemalloc.h: No such file or directory 在安装redis进行编译 ...

  3. 把一串数字表示成千位分隔形式——JS正则表达式的应用

    梳理思路 要先明白的是,我们将要转换成的数字格式是这样:从个位往左数起,每三位前插入一个千位分隔符,,即可以想象成我们要把每三位数字前面的那个空""匹配出来,并替换成千位分隔符,. ...

  4. java中六个时间类的使用和区别

    java.util.Date java.sql.Date   java.sql.Time   java.sql.Timestamp java.text.SimpleDateFormat java.ut ...

  5. FreeMarker之FTL指令

    assign指令 此指令用于在页面上定义一个变量 (1)定义简单类型: <#assign linkman="周先生"> 联系人:${linkman} (2)定义对象类型 ...

  6. 3.div+css 的布局较 table 布局有什么优点

    改版的时候更方便,只需改css文件. 页面加载速度更快.结构化清晰.页面显示简介. 表现与结构相分离. 已于优化搜索引擎更友好,排名更容易靠前.

  7. for循环、in、not in

    for循环可以遍历集合中任意一个元素 1 a = ["hello", "world", "dlrb"] 2 for b in a: 3 pr ...

  8. NAS 百科 —— http://baike.baidu.com/item/NAS%E7%BD%91%E7%BB%9C%E5%AD%98%E5%82%A8

    NAS(Network Attached Storage)网络存储基于标准网络协议实现数据传输,为网络中的Windows / Linux / Mac OS 等各种不同操作系统的计算机提供文件共享和数据 ...

  9. dbcp 连接池参数说明

    参考: http://commons.apache.org/proper/commons-dbcp/configuration.html https://www.cnblogs.com/happySm ...

  10. python __class__属性

    >>> class a(object): pass >>> o=a() >>> dir(o) ['__class__', '__delattr__ ...