Wednesday, October 01, 2008

Power of 2

I came across this link yesterday and found the book that the author mentioned pretty interesting. The book is Impossible?: Surprising Solutions to Counterintuitive Conundrums. Guess what, our excellent National Library has a number of copies available. Although most of the maths covered in the book are very hard for me to swallow, I found this "Power of 2" section very interesting from the viewpoint of programming (BTW, I am thinking in Python).

The original text in the book:

... the first power of 2 with 8 consecutive zeroes, July 1963, ... the authors provided what the note's title suggests:
Consecutive zerosPower of 2
253
3242
4377
51491
61492
76801
814007
the first power of 2 which contains precisely eight consecutive zeros. To be explicit, taking the first case, 253 = 9 007 199 254 740 992 ...

The Karst's IBM 1620 computer took 1 hour 18 mintues to find those eight consecutive zeros on 1 January 1964, ...

With Python, it is not difficult at all to do multi-precision integer arithmetic. Here is my version and it only took 46.67 seconds on my office notebook to compute up to eight consecutive zeros. My notebook (Dell Latitude D630 with Intel Core Duo CPU T7500 @ 2.20GHz) is 100.28 times faster than the IBM 1620! What would happen if I could bring this notebook back to 1960 ? Also, I am curious to know how many punch cards they required to do that calculation. My python script is only 18 lines (includes blank lines) and I am sure the factor for code base is more than 100 times. Now you get a glimpse of the advancement of both hardware and software over half a century.

$ cat power2.py
#! /usr/bin/python

import sys,time


num=1L
count=1L
start=time.time()
while True:
        try:
                if '0'*count in str(2**num):
                        print num, 'elapsed time: %f secs' % (time.time()-start)

                        count+=1
                num+=1
        except KeyboardInterrupt:
                print 'Calculated till:', num
                sys.exit(1)

$ ./power2.py
10 elapsed time: 0.000000 secs
53 elapsed time: 0.000000 secs
242 elapsed time: 0.015000 secs
377 elapsed time: 0.015000 secs
1491 elapsed time: 0.078000 secs
1492 elapsed time: 0.078000 secs
6801 elapsed time: 5.531000 secs
14007 elapsed time: 46.671000 secs

Labels:

0 Comments:

Post a Comment

<< Home