-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE014.py
More file actions
66 lines (46 loc) · 1.29 KB
/
PE014.py
File metadata and controls
66 lines (46 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
# -*coding:UTF-8-*-
"""
@Project hello_world
@File PE014.py
@Author Haosen Luo
@Date 2025/2/9 18:25
最长考拉兹序列
考虑如下定义在正整数集上的迭代规则:
N/2 (若N为偶数)
3N+1 (若N为奇数)
从13开始,可以迭代生成如下的序列:
可以看出这个序列(从13开始到1结束)共有项。尽管还未被证明,但普遍认为,从任何数开始最终都能抵达
并结束(这被称为“考拉兹猜想”)。
在小于一百万的数中,从哪个数开始迭代生成的序列最长?
注: 在迭代过程中允许出现超过一百万的项。
"""
def method(number, c=1):
"""
计算序列长度
:param number:
:return:
"""
if number == 1:
return c
else:
if number % 2 == 0:
number /= 2
else:
number = 3 * number + 1
c += 1
return method(number, c)
def main():
num = 13
total_num = 1000000
max_count = 1
for i in range(num, total_num):
count = method(i, c=1)
# print('count', count)
if max_count < count:
max_count = count
num = i
print(num, max_count)
print(f"最大序列长度为{max_count}, 值为{num}")
if __name__ == '__main__':
main()