import sys from time import clock """ There is another way of solving the problem by calculating octal equivalent of only first and last number and finding others using it. First of all, we'll find the number of digits of the last octal found. Then, for i in xrange(a,b): #a,b: first octal number, last octal number l=len(str(b)) Suppose if number of digits of last octal equivalent is 3. # l=3 if k%1000==778: k+=222 elif k%100==78: k+=22 elif k%10==8: k+=2 Series of octal will follow the above sequence. Similarly, depending on the number of digits, we can apply the checks to find the series. BUT this will be less efficient as there will be many checks in the program. Therefore, the following code has been used. """ def octal(): start=clock() countdiv=0 #to count the no. of divisions. f=open("in5_1.txt","r") f1=open("out_31_5_1.txt","w") st=f.read() f.close() str1=st.split(",") temp=[] a=int(str1[0]) b=int(str1[1])+1 app=temp.append #In python, local variables are easier to access than global ones. write=f1.write strg=str v=[] append=v.append for i in xrange(a,b): #xrange() is used instead of range() as it is a generator object which does not create any object immediately like range(), but rather when you pull from the generator. app(i) for j in temp: s="" a=1 while (a!= 0): a=j/8 b=j%8 countdiv+=1 j=a s=s+(strg(b)) s=s[::-1] append(s) write(",".join(v)) end=clock() write("\nThere are %d divisions and 1 addition in the program." %countdiv) write("\nTime taken by the program is : %f seconds" %(end-start)) mem=sys.getsizeof(f)+sys.getsizeof(f1)+sys.getsizeof(st)+sys.getsizeof(app)+sys.getsizeof(write)+sys.getsizeof(str1)+sys.getsizeof(a)+sys.getsizeof(b)+sys.getsizeof(strg)+sys.getsizeof(v)+sys.getsizeof(s)+sys.getsizeof(i)+sys.getsizeof(j)+sys.getsizeof(append) write("\nMemory used by the program is : %d bytes." %mem) f1.close() octal()