import sys from time import clock """ This can also be done by divide and conquer algorithm for power as follows: def pw(a,b): if b==0: return 1 elif b==1: return a elif b%2==0: return pw(a,b/2)*pw(a,b/2) else: return pw(a,b/2)*pw(a,b/2+1) But this program can be solved very efficiently using following rule. If the output series is a,b,c,d,e, then: c=a*b d=b*c e=c*d In this way the following program is written. """ def main(): countm=0 start=clock() f=open("in4_1.txt","r") f1=open("out_31_4_1.txt","w") s=f.read() f.close() s1=s.split(",") b=int(s1[1]) a=int(s1[0]) temp=[] write=f1.write append=temp.append b-=2 append("%d" %a) append("%d" %a) for i in xrange(0,b): x=int(temp[i]) y=int(temp[i+1]) s=x*y countm+=1 append("%d" %s) write("\n".join(temp)) end=clock() write("\nThere are %d multiplications and 1 substraction in the program." %countm) write("\nTime taken by the program is : %f seconds" %(end-start)) mem=sys.getsizeof(f)+sys.getsizeof(f1)+sys.getsizeof(s)+sys.getsizeof(s1)+sys.getsizeof(a)+sys.getsizeof(b)+sys.getsizeof(temp)+sys.getsizeof(write)+sys.getsizeof(append)+sys.getsizeof(i)+sys.getsizeof(s)+sys.getsizeof(x)+sys.getsizeof(y) write("\nMemory used by the program is : %d bytes." %mem) f1.close() main()