Sha256: 8ed02e4f1b89541a7b2f6802cd6522d3f9aa3aef53d2458eb456371dd8ab77ce

Contents?: true

Size: 542 Bytes

Versions: 13

Compression:

Stored size: 542 Bytes

Contents

class Integer
  require 'backports/tools/arguments'
  require 'backports/2.1.0/bignum/bit_length'
  require 'backports/2.1.0/fixnum/bit_length'

  def self.sqrt(n)
    n = Backports.coerce_to_int(n)
    return Math.sqrt(n).to_i if n <= 9_999_899_999_899_999_322_536_673_279
    bits_shift = n.bit_length/2 + 1
    bitn_mask = root = 1 << bits_shift
    while true
      root ^= bitn_mask if (root * root) > n
      bitn_mask >>= 1
      return root if bitn_mask == 0
      root |= bitn_mask
    end
  end
end unless Integer.respond_to? :sqrt

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/backports-3.12.0/lib/backports/2.5.0/integer/sqrt.rb
backports-3.16.1 lib/backports/2.5.0/integer/sqrt.rb
backports-3.16.0 lib/backports/2.5.0/integer/sqrt.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/backports-3.15.0/lib/backports/2.5.0/integer/sqrt.rb
backports-3.15.0 lib/backports/2.5.0/integer/sqrt.rb
backports-3.14.0 lib/backports/2.5.0/integer/sqrt.rb
backports-3.13.0 lib/backports/2.5.0/integer/sqrt.rb
backports-3.12.0 lib/backports/2.5.0/integer/sqrt.rb
backports-3.11.4 lib/backports/2.5.0/integer/sqrt.rb
backports-3.11.3 lib/backports/2.5.0/integer/sqrt.rb
backports-3.11.2 lib/backports/2.5.0/integer/sqrt.rb
backports-3.11.1 lib/backports/2.5.0/integer/sqrt.rb
backports-3.11.0 lib/backports/2.5.0/integer/sqrt.rb