[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Golf (7 strokes is par!)



Jeff Pinyan wrote:
> 
> Challenge: determine if $_ is an integral power of 2 (1,2,4,8,16,...)
> 
>   if (YOUR CODE HERE) { print "$_ is a power of 2" }
> 

Ok, uncle. :)  After spending my lunch hour on this beast, the best code
block I can come up with is:

     !($_&$_-1)

which by my count puts me 3 over.  'Course, if you were willing to
change the challenge to:

     if (YOUR CODE HERE) { print "$_ is NOT a power of 2" }

then $_&$_-1 would match your 7.  So I'll bite, what was your 7? (Of
course, you can send it to me privately if you don't want to spoil it
quite yet.)

----------------------------------------------------------------
Jeff Helman                 Product Manager -- Internet Services
jhelman@wsb.com                    CCH Washington Service Bureau
----------------------------------------------------------------

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe