import sys from time import clock def main(): start=clock() countm=0 #to count the no. ofmultiplications. counta=0 #to count the no. of additions. f=open("in1_1.txt","r") f1=open("out_31_1_1.txt","w") temp=[] append=temp.append #In python, local variables are easier to access than global ones. If temp.append() is invoked multiple times, it will call the append function from the library everytime. #Therefore, using local variable, append() will be call from library just once and later using the local variable.""" write=f1.write for x in f: for y in x.split(','): y=y.strip() append(y) f.close() temp=temp[1::] for i in temp: i=int(i) k=i+1 counta+=1 for j in temp: j=int(j) if i>j: t=i t=int(t)+1 counta+=1 f=1 for i in xrange(2,t): #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. f2=f for l in xrange(1,i): f=f+f2 counta+=1 for j in temp: j=int(j) if i==j: write("%d=%d\n" %(i,f)) end=clock() write("There are %d additions in this program." %counta) write("\nTime taken by the program is : %f seconds" %(end-start)) mem=sys.getsizeof(f)+sys.getsizeof(f1)+sys.getsizeof(temp)+sys.getsizeof(append)+sys.getsizeof(write)+sys.getsizeof(x)+sys.getsizeof(y)+sys.getsizeof(i)+sys.getsizeof(k)+sys.getsizeof(j)+sys.getsizeof(t)+sys.getsizeof(f2) write("\nMemory used by the program is : %d bytes." %mem) f1.close() main()