Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer sqrt and nth roots #8920

Open
jzakiya opened this issue Mar 20, 2020 · 1 comment
Open

Integer sqrt and nth roots #8920

jzakiya opened this issue Mar 20, 2020 · 1 comment

Comments

@jzakiya
Copy link

jzakiya commented Mar 20, 2020

Initiated in forum: https://forum.crystal-lang.org/t/integer-sqrt-in-stdlib/1839/8

Using Math.sqrt(n).to_i provides inaccurate results once n reaches a certain large size,
rendering this operation useless for numerical algorithms/processing using large numbers.
Including methods for Integer sqrt, and nth roots makes Crystal much more useable for
numerical applications (which Julia, et al, promote as its claim-to-fame).

require "big"

module IntRoots

  def isqrt(n = self)   # Newton's method for integer sqrt
    raise "ivalid negative integer" if n < 0
    return n if n < 2
    b = n.to_s(2).size
    one = typeof(self).new(1)  # value 1 of type self
    x = one << ((b-1) >> 1) | n >> ((b >> 1) + 1)
    while (t = n // x) < x; x = (x + t) >> 1 end
    x
  end

  def irootn(n)        # Newton's method for integer nth root
    return nil if self < 0 && n.even?
    raise "root n not an Integer >= 2" unless n.is_a?(Int) && n > 1
    return self if self == 0 || (self == -1 && n.odd?)
    num = self.abs
    one = typeof(self).new(1)  # value 1 of type self
    e, x = n-1, one << (num.to_s(2).size-1) // n + 1
    while (t = (e * x + num // x ** e) // n) < x
      x = t 
    end
    x *= self < 0 ? -1 : 1
  end
end

struct Int; include IntRoots end

There are multiple implementation details that need to be agreed upon (and documented) to present a consistent name and use API to users, but other than that, the code above provides working implementations that can be a starting point for optimization.

@HertzDevil
Copy link
Contributor

Partially resolved by #10549.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants