Isqrt (integer square root) of X: Difference between revisions
→{{header|Ruby}}: Use refinement instead of global monkey-patching of Integer class
(→{{header|Haskell}}: added solution) |
(→{{header|Ruby}}: Use refinement instead of global monkey-patching of Integer class) |
||
Line 3,137:
=={{header|Ruby}}==
Ruby already has [https://ruby-doc.org/core-2.7.0/Integer.html#method-c-sqrt Integer.sqrt], which results in the integer square root of a positive integer. It can be re-implemented as follows:
<lang ruby>
refine Integer do
def commatize
self.to_s.gsub( /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1,\\2")
end
end
end
using Commatize
def isqrt(x)
q, r = 1, 0
|