File: sqrt.rb

package info (click to toggle)
ruby-backports 3.25.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,912 kB
  • sloc: ruby: 11,759; makefile: 6
file content (18 lines) | stat: -rw-r--r-- 541 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
    loop do
      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