Why Can't This Code Find Powers? (Ruby)
问题 App Academy's practice test says their chosen way of finding if an input is a power of 2 is to keep dividing it by 2 on a loop and check whether the end result is 1 or 0 (after having tested for the numbers 1 and 0 as inputs), which makes sense, but why won't this way work? def try gets(num) counter = 0 go = 2 ** counter if num % go == 0 return true else counter = counter + 1 end return false end I can't figure out why this won't work, unless the counter isn't working. 回答1: There are a number