1 of the most famous unsolved math sequences, the aim of this task is this see how many steps it takes for n (positive integer) to end to 1.
The 2 rules of the Conjecture is that if n is even, divide it by 2 and if n is odd, n will equal 3n + 1
for example,
if n = 4,
you would divide n by 2 to get 2
then, u would divide 2 by 2 again to get 1
once you reach one, you check how many steps it took to get to 1. In this case, it would be 2 so return 2.
def collatz(n):
attempts = 0
while n != 1:
if n % 2 == 0:
n /= 2
attempts += 1
else:
n = (3*n) + 1
attempts += 1
return attempts
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(collatz(4), 2)
test.assert_equals(collatz(12), 9)
test.assert_equals(collatz(821), 28)
test.assert_equals(collatz(3591), 149)
test.assert_equals(collatz(324242), 215)