Brain Teaser: Multiply by 6

How do I go about multiplying a real number n by 6 in python without using the * or the + operators?

Solution:

x6 = lambda n: (n<<3) - n - n

Rationale:

n<<3 is equivalent to multiplying n by 2**3 or 8n. To reach 6n, we simply subtract 2n.

This entry was posted in Blog, Programming, Python, Snippets and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>