mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-17 14:58:10 +00:00
Added 3n+1
This commit is contained in:
parent
4e0184a41d
commit
dd8e2afae6
14
Maths/3n+1.py
Normal file
14
Maths/3n+1.py
Normal file
|
@ -0,0 +1,14 @@
|
|||
def n31(a):# a = initial number
|
||||
c = 0
|
||||
l = [a]
|
||||
while a != 1:
|
||||
if a % 2 == 0:#if even divide it by 2
|
||||
a = a // 2
|
||||
elif a % 2 == 1:#if odd 3n+1
|
||||
a = 3*a +1
|
||||
c += 1#counter
|
||||
l += [a]
|
||||
print(a)#optional print
|
||||
print("It took {0} steps.".format(c))#optional finish
|
||||
return l , c
|
||||
print(n31(43))
|
Loading…
Reference in New Issue
Block a user