Skip to content

Commit 5170853

Browse files
Update 3n_plus_1.py (TheAlgorithms#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>
1 parent 7f1a552 commit 5170853

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

maths/3n_plus_1.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def n31(a: int) -> tuple[list[int], int]:
1111
if not isinstance(a, int):
1212
raise TypeError(f"Must be int, not {type(a).__name__}")
1313
if a < 1:
14-
raise ValueError(f"Given integer must be greater than 1, not {a}")
14+
raise ValueError(f"Given integer must be positive, not {a}")
1515

1616
path = [a]
1717
while a != 1:
1818
if a % 2 == 0:
19-
a = a // 2
19+
a //= 2
2020
else:
2121
a = 3 * a + 1
22-
path += [a]
22+
path.append(a)
2323
return path, len(path)
2424

2525

0 commit comments

Comments
 (0)