Thursday, March 01, 2007

Follow up: A fun way to multiply numbers

Here's python code to multiply numbers as mentioned in the A fun way to multiply numbers blog entry.

NOTE: I am not a python expert, so there could be some obvious bugs in this simple piece of code. As always use at your own risk.


print "Input the first number"
a=input()
print "Input the next number to be multiplied with %d" %a
b=input()
res=0
while a:
if a & 0x1:
res = res + b
a = a >> 1
b = b << 1

print "The result is %d\n" %res

No comments:

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...