mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Simplify code by dropping support for legacy Python (#1143)
* Simplify code by dropping support for legacy Python * sort() --> sorted()
This commit is contained in:
@ -16,20 +16,12 @@ that all starting numbers finish at 1.
|
||||
|
||||
Which starting number, under one million, produces the longest chain?
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def solution(n):
|
||||
"""Returns the number under n that generates the longest sequence using the
|
||||
formula:
|
||||
n → n/2 (n is even)
|
||||
n → 3n + 1 (n is odd)
|
||||
|
||||
|
||||
# The code below has been commented due to slow execution affecting Travis.
|
||||
# >>> solution(1000000)
|
||||
# {'counter': 525, 'largest_number': 837799}
|
||||
@ -62,7 +54,7 @@ def solution(n):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = solution(int(raw_input().strip()))
|
||||
result = solution(int(input().strip()))
|
||||
print(
|
||||
(
|
||||
"Largest Number:",
|
||||
|
@ -24,14 +24,6 @@ that all starting numbers finish at 1.
|
||||
|
||||
Which starting number, under one million, produces the longest chain?
|
||||
"""
|
||||
from __future__ import print_function
|
||||
|
||||
try:
|
||||
raw_input # Python 2
|
||||
except NameError:
|
||||
raw_input = input # Python 3
|
||||
|
||||
|
||||
def collatz_sequence(n):
|
||||
"""Returns the Collatz sequence for n."""
|
||||
sequence = [n]
|
||||
@ -63,7 +55,7 @@ def solution(n):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
result = solution(int(raw_input().strip()))
|
||||
result = solution(int(input().strip()))
|
||||
print(
|
||||
"Longest Collatz sequence under one million is %d with length %d"
|
||||
% (result["largest_number"], result["counter"])
|
||||
|
Reference in New Issue
Block a user