Skip to content

Instantly share code, notes, and snippets.

@kidinov
Created May 12, 2015 15:18
Show Gist options
  • Save kidinov/5a376b98b6c49a2ffb18 to your computer and use it in GitHub Desktop.
Save kidinov/5a376b98b6c49a2ffb18 to your computer and use it in GitHub Desktop.
public static void main(String[] args) {
System.out.println(String.format("10 is power of two = %s", isPowerOfTwo(10)));
System.out.println(String.format("8 is power of two = %s", isPowerOfTwo(8)));
System.out.println(String.format("-3 is power of two = %s", isPowerOfTwo(-3)));
System.out.println(String.format("3.14 is power of two = %s", isPowerOfTwo(3.14f)));
}
public static boolean isPowerOfTwo(float arg) {
if (Math.floor(arg) != arg) {
return false;
}
if (arg == 1) {
return true;
}
return isPowerOfTwo(arg / 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment