7 kyu

Power of two

46 of 25,695yuriy.tsemashko

Description:

Complete the function power_of_two/powerOfTwo (or equivalent, depending on your language) that determines if a given non-negative integer is a power of two. From the corresponding Wikipedia entry:

a power of two is a number of the form 2n where n is an integer, i.e. the result of exponentiation with number two as the base and integer n as the exponent.

You may assume the input is always valid.

Examples

power_of_two?(1024) # true
power_of_two?(4096) # true
power_of_two?(333)  # false
power_of_two(1024) ==> True
power_of_two(4096) ==> True
power_of_two(333)  ==> False
PowerOfTwo.isPowerOfTwo(1024) // -> true
PowerOfTwo.isPowerOfTwo(4096) // -> true
PowerOfTwo.isPowerOfTwo(333)  // -> false
isPowerOfTwo(1024) // -> true
isPowerOfTwo(4096) // -> true
isPowerOfTwo(333)  // -> false
poweroftwo(1024) # true
poweroftwo(4096) # true
poweroftwo(333)  # false
isPowerOfTwo       1 `shouldBe` True
isPowerOfTwo       2 `shouldBe` True
isPowerOfTwo       6 `shouldBe` False
isPowerOfTwo       8 `shouldBe` True
isPowerOfTwo    1024 `shouldBe` True
isPowerOfTwo    1026 `shouldBe` False
powerOfTwo 1 `shouldEqual` true
powerOfTwo 2 `shouldEqual` true
powerOfTwo 6 `shouldEqual` false
powerOfTwo 8 `shouldEqual` true
powerOfTwo 1024 `shouldEqual` true
powerOfTwo 1026 `shouldEqual` false
power_of_two(0);     // returns false
power_of_two(16);    // returns true
power_of_two(100);   // returns false
power_of_two(1024);  // returns true
power_of_two(20000); // returns false
power_of_two(0); // false
power_of_two(1); // true
power_of_two(2); // true
power_of_two(37); // false
power_of_two(64); // true

Beware of certain edge cases - for example, 1 is a power of 2 since 2^0 = 1 and 0 is not a power of 2.

Mathematics
Algorithms
Fundamentals

Similar Kata:

Stats:

CreatedApr 15, 2014
PublishedApr 15, 2014
Warriors Trained37641
Total Skips2465
Total Code Submissions120683
Total Times Completed25695
Ruby Completions1622
JavaScript Completions14106
Python Completions6409
Haskell Completions464
C# Completions933
C Completions969
TypeScript Completions551
PureScript Completions46
Rust Completions668
Julia Completions66
NASM Completions92
COBOL Completions14
Java Completions681
Total Stars275
% of votes with a positive feedback rating90% of 2242
Total "Very Satisfied" Votes1862
Total "Somewhat Satisfied" Votes314
Total "Not Satisfied" Votes66
Ad
Contributors
  • yuriy.tsemashko Avatar
  • jhoffner Avatar
  • xcthulhu Avatar
  • ZozoFouchtra Avatar
  • bkaes Avatar
  • Unnamed Avatar
  • asmgf Avatar
  • GiacomoSorbi Avatar
  • Jotha Avatar
  • temo Avatar
  • anter69 Avatar
  • donaldsebleung Avatar
  • imjasonmiller Avatar
  • kazk Avatar
  • Voile Avatar
  • Souzooka Avatar
  • rowcased Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • solitude Avatar
  • OpenByte Avatar
  • qwertyu123 Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
Ad