mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Update 3n_plus_1.py (#7966)
* Update 3n_plus_1.py 1. Minor issue with ValueError message: Given integer should be positive, not greater than 1, as 1 is allowed. 2. += calls underlying list extend method which might be slower. Calling apend seems more appropriate. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
7f1a5521f4
commit
51708530b6
@ -11,15 +11,15 @@ def n31(a: int) -> tuple[list[int], int]:
|
|||||||
if not isinstance(a, int):
|
if not isinstance(a, int):
|
||||||
raise TypeError(f"Must be int, not {type(a).__name__}")
|
raise TypeError(f"Must be int, not {type(a).__name__}")
|
||||||
if a < 1:
|
if a < 1:
|
||||||
raise ValueError(f"Given integer must be greater than 1, not {a}")
|
raise ValueError(f"Given integer must be positive, not {a}")
|
||||||
|
|
||||||
path = [a]
|
path = [a]
|
||||||
while a != 1:
|
while a != 1:
|
||||||
if a % 2 == 0:
|
if a % 2 == 0:
|
||||||
a = a // 2
|
a //= 2
|
||||||
else:
|
else:
|
||||||
a = 3 * a + 1
|
a = 3 * a + 1
|
||||||
path += [a]
|
path.append(a)
|
||||||
return path, len(path)
|
return path, len(path)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user