Isqrt (integer square root) of X: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: added solution) |
(→{{header|Ruby}}: Use refinement instead of global monkey-patching of Integer class) |
||
Line 3,137: | Line 3,137: | ||
=={{header|Ruby}}== |
=={{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: |
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> |
<lang ruby>module Commatize |
||
refine Integer do |
|||
def commatize |
def commatize |
||
self.to_s.gsub( /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1,\\2") |
self.to_s.gsub( /(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/, "\\1,\\2") |
||
end |
|||
end |
end |
||
end |
end |
||
using Commatize |
|||
def isqrt(x) |
def isqrt(x) |
||
q, r = 1, 0 |
q, r = 1, 0 |