RosettaCodeData/Task/Isqrt-integer-square-root-of-X/Ruby/isqrt-integer-square-root-of-x.rb
2023-07-01 13:44:08 -04:00

25 lines
458 B
Ruby

module Commatize
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
while (q <= x) do q <<= 2 end
while (q > 1) do
q >>= 2; t = x-r-q; r >>= 1
if (t >= 0) then x, r = t, r+q end
end
r
end
puts (0..65).map{|n| isqrt(n) }.join(" ")
1.step(73, 2) do |n|
print "#{n}:\t"
puts isqrt(7**n).commatize
end