- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
程序应该做的是采取步骤和数字,然后输出您有多少个独特的序列,正好有 x 个 steps
来创建 number
。
有人知道我可以如何节省一些内存吗 - 因为我应该在 4 秒的限制内为相当大的数字工作。
def IsaacRule(steps, number):
if number in IsaacRule.numbers:
return 0
else:
IsaacRule.numbers.add(number)
if steps == 0:
return 1
counter = 0
if ((number - 1) / 3) % 2 == 1:
counter += IsaacRule(steps-1, (number - 1) / 3)
if (number * 2) % 2 == 0:
counter += IsaacRule(steps-1, number*2)
return counter
IsaacRule.numbers = set()
print(IsaacRule(6, 2))
如果有人知道带内存的版本,我将不胜感激,现在它可以工作,但仍有改进的余地。
最佳答案
基线:IsaacRule(50, 2)
需要 6.96 秒
这使代码花费了更长的时间,并给出了不同的最终结果
(number * 2) % 2 == 0
to True
IsaacRule(50, 2)
需要 0.679 秒。感谢 Pm2Ring 的这个。
((number - 1)/3) % 2 == 1
简化为 number % 6 == 4
并尽可能使用楼层划分:IsaacRule(50, 2)
耗时 0.499s
真值表:
| n | n-1 | (n-1)/3 | (n-1)/3 % 2 | ((n-1)/3)%2 == 1 |
|---|-----|---------|-------------|------------------|
| 1 | 0 | 0.00 | 0.00 | FALSE |
| 2 | 1 | 0.33 | 0.33 | FALSE |
| 3 | 2 | 0.67 | 0.67 | FALSE |
| 4 | 3 | 1.00 | 1.00 | TRUE |
| 5 | 4 | 1.33 | 1.33 | FALSE |
| 6 | 5 | 1.67 | 1.67 | FALSE |
| 7 | 6 | 2.00 | 0.00 | FALSE |
代码:
def IsaacRule(steps, number):
if number in IsaacRule.numbers:
return 0
else:
IsaacRule.numbers.add(number)
if steps == 0:
return 1
counter = 0
if number % 6 == 4:
counter += IsaacRule(steps-1, (number - 1) // 3)
counter += IsaacRule(steps-1, number*2)
return counter
IsaacRule(50, 2)
耗时 0.381s
这让我们可以利用对集合所做的任何优化。基本上我在这里进行广度优先搜索。
IsaacRule(50, 2)
需要 0.256 秒
我们只需要添加一个检查 number != 1
来打破唯一已知的循环。这会加快速度,但如果您从 1 开始,则需要添加一个特殊情况。感谢 Paul 的建议!
START = 2
STEPS = 50
# Special case since we broke the cycle
if START == 1:
START = 2
STEPS -= 1
current_candidates = {START} # set of states that can be reached in `step` steps
for step in range(STEPS):
# Get all states that can be reached from current_candidates
next_candidates = set(number * 2 for number in current_candidates if number != 1) | set((number - 1) // 3 for number in current_candidates if number % 6 == 4)
# Next step of BFS
current_candidates = next_candidates
print(len(next_candidates))
关于python反Collatz猜想,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51537946/
我有两个表:tab1 和 tab2。每个表只有一个 VARCHAR(MAX) 列。 我只需要获取那些值对,它们是相等的,但只有大小写不同。 示例输入: tab1.t1 tab2.t2 ----
我正在尝试通过执行 [[self navigationController] popViewControllerAnimated:YES] 来“自毁” View Controller 。 在我这样做之
我在开发存储过程时收到此错误 Implicit conversion of varchar value to varchar cannot be performed because the colla
我正在浏览ICU源代码(http://icu-project.org/),但是找不到用于排序的开箱即用语言。有人可以帮我吗? 最佳答案 编辑:请注意,此列表是几年前写的。单击链接以获取更新的列表。 C
django==1.11.9 mysql版本5.6.38 +----------------------+-----------------+ | Variable_name | Val
我们使用 couchbase 作为我们的 nosql 存储并喜欢它的功能。 然而,我们在创建关联时遇到了一个问题 通过 View 整理。这可以被认为类似于连接操作。 虽然我们的数据集是 secret
cmp operator适用于代码编号,或者至少我认为它是这样做的,因为文档没有明确说明并且没有提及任何本地化内容。 我可以让它按其他排序规则排序吗?我知道我告诉 sort如何比较,但我认为它必须已经
我需要捕获 Windows 计算机已设置的打印机驱动程序设置,以确定它们是否已为特定打印机打开逐份打印功能。 我知道如何使用传入 DC_COLLATE 的 DeviceCapability 调用来获取
我刚刚开始学习Phinx,我需要将列的排序规则更改为latin1_swedish_ci。我没有找到有关如何执行此操作的文档。我假设它类似于: ->addColumn('text_two', 'stri
我在 SQL Server 2008 R2 dev 上,服务器默认排序规则是 Cyrillic_General_CI_AS 在 SSMS 中执行 选择 'éÉâÂàÀëËçæà' COLLATE La
我收到以下错误消息 [Err] 1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,C
我刚刚导出了一个 MySQL 数据库,以便在另一台服务器上复制它。查看 sql 脚本,我看到以下内容: CREATE TABLE `X` ( `id` int(11) unsigned NOT N
MYSQL 数据库 数据库 1 - 表 - 表 1 id - 整数 标题 - utf8_general_ci 值(value)观 - 1、Bienvenue Chez les Châtis 2、基础知
当我使用 phpMyAdmin 查看我的在线表格时,它使用整理方法“latin_swedish_ce”。为什么?默认值是多少?以及,使用什么整理方法? 最佳答案 MySQL 中的一个字符串有一个cha
我有一个名为 username 的列,我希望用户能够插入日语、罗马语、阿拉伯语、韩语以及所有可能的文本,包括特殊字符 [ https://en.wiktionary.org/wiki/Index:Al
我想比较 SQLite 数据库中的两个字符串而不关心重音和大小写。我的意思是“Événement”应该等于“evenèment”。 在 Debian Wheezy 上,SQLite 包不提供 ICU。
我在本地机器上使用提供的 COLLATE 值进行排序时遇到问题(在服务器上一切正常)。似乎没有任何效果。 使用 en_US COLLATE 的示例 SQL 代码: SELECT username FR
在 Microsoft SQL Server 中,如果我想在区分大小写的数据库中不区分大小写搜索,可以运行以下 SQL: SELECT * FROM MyTable WHERE MyField = '
我正在使用 mongoose paginate v2 从版本 3.2.7 的 mongodb 集合中检索文档。 var options = { page, limit }; Schema.pagina
有没有办法在 SQLITE 中使用“IN”子句,该子句将敏感地将列与 in 子句中的所有元素进行匹配。我有一个疑问 select * from Table where table.column in
我是一名优秀的程序员,十分优秀!