From 9843926dff35ac4506af87ac8826b262cfdb46f9 Mon Sep 17 00:00:00 2001 From: Kalpak Take Date: Sun, 9 Jul 2017 08:15:08 +0530 Subject: [PATCH] Made more smaller and less memory utilization --- collatz_sequence.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/collatz_sequence.py b/collatz_sequence.py index 523c60f..f3c64c7 100644 --- a/collatz_sequence.py +++ b/collatz_sequence.py @@ -15,14 +15,13 @@ def collatz(num): # defining the function else: return 'Enter a valid integer' - while (res == 1): #we need to exit the function after 1 is obtained - print(res) - exit(0) - - while (res != 1): - print(res) - num = res - collatz(num) #the function should be executing until 1 is obtained + while True: + if (res == 1): #we need to exit the function after 1 is obtained + print(res) + exit(0) + elif (res != 1): + print(res) + collatz(res) #the function should be executing until 1 is obtained try: num = int(input('Enter an integer: ')) #asking for input and then calling the function also change input to raw_input in python 2x versions