Last active
November 26, 2019 11:16
-
-
Save dhirabayashi/500eb3d188ff570cc2553df98589f45e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'prime' | |
def power?(n) | |
if n <= 0 | |
return false | |
end | |
if n == 1 | |
return true | |
end | |
ans = n.prime_division | |
ans.size == 1 and ans[0][0] == 2 | |
end | |
(1..1024).each do |n| | |
puts n if power?(n) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def power?(n) | |
if n <= 0 | |
return false | |
end | |
if n == 1 | |
return true | |
end | |
if n.odd? | |
return false | |
end | |
ans = n | |
while (ans /= 2) != 0 | |
return false if ans != 1 and ans.odd? | |
end | |
true | |
end | |
(1..1024).each do |n| | |
puts n if power?(n) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def power?(n) | |
if n <= 0 | |
false | |
else | |
(n & (n-1)) == 0 | |
end | |
end | |
(1..1024).each do |n| | |
puts n if power?(n) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment