정규식 (ECMAScript), 85 73 71 바이트
^((?=(x*?)\2(\2{4})+$|(x*?)(\4\4xx)*$)(\2\4|(x*)\5\7\7(?=\4\7$\2)\B))*$
온라인으로 사용해보십시오!
Deadcode의 설명
이전 73 바이트 버전이 아래에 설명되어 있습니다.
^((?=(x*?)\2(\2{4})+$)\2|(?=(x*?)(\4\4xx)*$)(\4|\5(x*)\7\7(?=\4\7$)\B))+$
ECMAScript 정규식의 한계로 인해 효과적인 전술은 종종 필요한 속성을 모든 단계에서 변하지 않고 유지하면서 한 번에 한 단계 씩 변환하는 것입니다. 예를 들어, 완벽한 제곱 또는 2의 거듭 제곱을 테스트하려면 모든 단계에서 제곱 또는 2의 거듭 제곱 (각각)을 유지하면서 크기를 줄이십시오.
이 솔루션이 모든 단계에서 수행하는 작업은 다음과 같습니다.
1
1
1
10
01
01
ones>zeroes1
ones>zeroes⇔ones−1>zeroes−1
이러한 반복 된 단계가 더 이상 진행되지 않으면, 최종 결과는 연속적인 1
비트 열이 될 것이며 , 이는 원래 숫자도 무거웠 음을 나타내거나 2의 거듭 제곱은 원래 숫자가 무겁지 않다는 것을 나타냅니다.
물론, 이러한 단계는 숫자의 이진 표현에 대한 인쇄상의 조작의 관점에서 위에서 설명되었지만 실제로는 단항 산술로 구현됩니다.
# For these comments, N = the number to the right of the "cursor", a.k.a. "tail",
# and "rightmost" refers to the big-endian binary representation of N.
^
( # if N is even and not a power of 2:
(?=(x*?)\2(\2{4})+$) # \2 = smallest divisor of N/2 such that the quotient is
# odd and greater than 1; as such, it is guaranteed to be
# the largest power of 2 that divides N/2, iff N is not
# itself a power of 2 (using "+" instead of "*" is what
# prevents a match if N is a power of 2).
\2 # N = N - \2. This changes the rightmost "10" to a "01".
| # else (N is odd or a power of 2)
(?=(x*?)(\4\4xx)*$) # \4+1 = smallest divisor of N+1 such that the quotient is
# odd; as such, \4+1 is guaranteed to be the largest power
# of 2 that divides N+1. So, iff N is even, \4 will be 0.
# Another way of saying this: \4 = the string of
# contiguous 1 bits from the rightmost part of N.
# \5 = (\4+1) * 2 iff N+1 is not a power of 2, else
# \5 = unset (NPCG) (iff N+1 is a power of 2), but since
# N==\4 iff this is the case, the loop will exit
# immediately anyway, so an unset \5 will never be used.
(
\4 # N = N - \4. If N==\4 before this, it was all 1 bits and
# therefore heavy, so the loop will exit and match. This
# would work as "\4$", and leaving out the "$" is a golf
# optimization. It still works without the "$" because if
# N is no longer heavy after having \4 subtracted from it,
# this will eventually result in a non-match which will
# then backtrack to a point where N was still heavy, at
# which point the following alternative will be tried.
|
# N = (N + \4 - 2) / 4. This removes the rightmost "01". As such, it removes
# an equal number of 0 bits and 1 bits (one of each) and the heaviness of N
# is invariant before and after. This fails to match if N is a power of 2,
# and in fact causes the loop to reach a dead end in that case.
\5 # N = N - (\4+1)*2
(x*)\7\7(?=\4\7$) # N = (N - \4) / 4 + \4
\B # Assert N > 0 (this would be the same as asserting N > 2
# before the above N = (N + \4 - 2) / 4 operation).
)
)+
$ # This can only be a match if the loop was exited due to N==\4.