mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Some grammatical and spelling corrections (#1475)
This commit is contained in:
parent
a57809af9c
commit
bc52aa6d4d
@ -1,16 +1,16 @@
|
|||||||
def collatz_sequence(n):
|
def collatz_sequence(n):
|
||||||
"""
|
"""
|
||||||
Collatz conjecture: start with any positive integer n.Next termis obtained from the previous term as follows:
|
Collatz conjecture: start with any positive integer n.Next term is obtained from the previous term as follows:
|
||||||
if the previous term is even, the next term is one half the previous term.
|
if the previous term is even, the next term is one half of the previous term.
|
||||||
If the previous term is odd, the next term is 3 times the previous term plus 1.
|
If the previous term is odd, the next term is 3 times the previous term plus 1.
|
||||||
The conjecture states the sequence will always reach 1 regaardess of starting n.
|
The conjecture states the sequence will always reach 1 regaardless of starting value n.
|
||||||
Example:
|
Example:
|
||||||
>>> collatz_sequence(43)
|
>>> collatz_sequence(43)
|
||||||
[43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
|
[43, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
|
||||||
"""
|
"""
|
||||||
sequence = [n]
|
sequence = [n]
|
||||||
while n != 1:
|
while n != 1:
|
||||||
if n % 2 == 0: # even
|
if n % 2 == 0: # even number condition
|
||||||
n //= 2
|
n //= 2
|
||||||
else:
|
else:
|
||||||
n = 3 * n + 1
|
n = 3 * n + 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user