16_dragon_checksum.rb
# ones in the inclusive one-indexed range [left, right]
# currently unused in this solution, but historically significant
ones in the inclusive one-indexed range [left, right]
currently unused in this solution, but historically significant
ones in the inclusive one - indexed range [ left right ] currently unused in this solution but historically significant
def ones(left, right)
    zero = 1 << Math.log2(right).floor
    if left > zero
      len = right - left + 1
      return len - ones(zero * 2 - right, zero * 2 - left)
    end
    left_of_zero = zero - left
    right_of_zero = right - zero
    overlap = [left_of_zero, right_of_zero].min
    excess = (left_of_zero - right_of_zero).abs
    if left_of_zero > right_of_zero
      overlap + ones(left, zero - 1 - overlap)
    elsif left_of_zero < right_of_zero
      overlap + excess - ones(zero * 2 - right, left - 1)
    else
      overlap
    end
  end
def ones ( left , right ) zero = 1 << Math . log2 ( right ) . floor if left > zero len = right - left + 1 return len - ones ( zero * 2 - right , zero * 2 - left ) end left_of_zero = zero - left right_of_zero = right - zero overlap = [ left_of_zero , right_of_zero ] . min excess = ( left_of_zero - right_of_zero ) . abs if left_of_zero > right_of_zero overlap + ones ( left , zero - 1 - overlap ) elsif left_of_zero < right_of_zero overlap + excess - ones ( zero * 2 - right , left - 1 ) else overlap end end
ones in the inclusive one-indexed range [left, right]
currently unused in this solution, but historically significant
ones in the inclusive one - indexed range [ left right ] currently unused in this solution but historically significant
# Powers of two are guaranteed zero. # Find the largest one no larger than the right end. # we are completely on one end of the power of two. # we straddle the power of two.
left
<not_specific>
right
<not_specific>