Thursday 2 July 2009

Joshua Bloch: Here's a Mystic Code Poem

The Mystic Code Poem from Joshua Bloch could be written down in an even more mystical and funny way:
int inverse(int __) { 
  int _=__; 
 
  _*= -~-~- (__*_); 
  _*= -~-~- (__*_); 
  _*= -~-~- (__*_); 
  _*= -~-~- (__*_); 
 
  return _; 
}
Not quite obvious, but the code above will find the multiplicative inverse of its odd parameter, e.g. N*inverse(N) = 1 mod 2^32.