python - 有没有办法将打印限制为只有真正的一致性?
问题描述
我正在研究这段代码来计算费马小定理,它应该可以正常工作。我唯一的问题是我希望它更有效率。有没有办法将打印限制为只有真正的一致性?
for i in range (1,351):
print i, [(2 << i - 2) % i == 1]
解决方案
这段代码甚至对我不起作用,它给出了一个错误:ValueError: negative shift count
. 但考虑到它以某种方式为您工作,您可以使用if
条件仅在 true 时打印:
for i in range (2,351): # changing 1 to 2 fixed the error for me.
if (2 << i-2) % i == 1: # this will check if it's true, then only print
print i, [(2 << i - 2) % i == 1]
推荐阅读
- aggregation-framework - 关于springdata mongodb聚合
- amazon-web-services - AWS websockets:断开roomIds
- c# - 我有一个父子列表,我想在下面提到的父子 json 中转换该列表
- android - 当以多种语言搜索其名称时,我们如何使应用程序出现在 Google Play 上
- unit-testing - 在访问日志中间件的情况下如何进行单元测试
- python - 一个简单的矩阵乘法会引发错误“形状 (2,3) 和 (2,3) 未对齐:3 (dim 1) != 2 (dim 0)”?
- java - 需要帮助将 OAuth2 安全性添加到 Spring Boot 微服务
- sql - 我如何知道我的 ADO 管道正在使用哪个用户?
- python - Python上两个数字的最大公共除法器
- jquery - 如何使用 jQuery DataTables 传递参数并重绘表格