Here's a simple one with some options for efficiency. Create a function that takes in a number and returns whether or not the number is a power of 2 or not.
Here's a simple one with some options for efficiency. Create a function that takes in a number and returns whether or not the number is a power of 2 or not.
Permalink: http://problemotd.com/problem/power-of-2/
Content curated by @MaxBurstein
Comments:
john smith - 10 years, 6 months ago
java example
I gess its simple.
reply permalink
Bryan St. Amour - 10 years, 6 months ago
In C:
int power(int n) { return ! (n & (n - 1) ); }
reply permalink
Nick Krichevsky - 10 years, 6 months ago
Python
reply permalink
Anonymous - 10 years, 6 months ago
reply permalink
Ethan - 10 years, 6 months ago
reply permalink
Ethan - 10 years, 6 months ago
I change my mind (thanks Nick):
https://gist.github.com/eglove/5f17f098a99f3bca41eb
reply permalink
Matthew - 10 years, 6 months ago
Python (2 or 3):
reply permalink
Max Burstein - 10 years, 6 months ago
Didn't know bin was a thing in python. Good stuff
reply permalink