From dd8e2afae6b808237c0a9b405aa5d544131ad87f Mon Sep 17 00:00:00 2001 From: gerroo Date: Sun, 28 Oct 2018 13:23:19 -0800 Subject: [PATCH] Added 3n+1 --- Maths/3n+1.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Maths/3n+1.py diff --git a/Maths/3n+1.py b/Maths/3n+1.py new file mode 100644 index 00000000..e1b82d39 --- /dev/null +++ b/Maths/3n+1.py @@ -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))