How to resolve the algorithm Isqrt (integer square root) of X step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Isqrt (integer square root) of X step by step in the Ruby programming language

Table of Contents

Problem Statement

Sometimes a function is needed to find the integer square root of   X,   where   X   can be a real non─negative number. Often   X   is actually a non─negative integer. For the purposes of this task,   X   can be an integer or a real number,   but if it simplifies things in your computer programming language,   assume it's an integer.

One of the most common uses of   Isqrt   is in the division of an integer by all factors   (or primes)   up to the   √ X    of that integer,   either to find the factors of that integer,   or to determine primality.

An alternative method for finding the   Isqrt   of a number is to calculate:       floor( sqrt(X) )

If the hardware supports the computation of (real) square roots,   the above method might be a faster method for small numbers that don't have very many significant (decimal) digits. However, floating point arithmetic is limited in the number of   (binary or decimal)   digits that it can support.

For this task, the integer square root of a non─negative number will be computed using a version of   quadratic residue,   which has the advantage that no   floating point   calculations are used,   only integer arithmetic. Furthermore, the two divisions can be performed by bit shifting,   and the one multiplication can also be be performed by bit shifting or additions. The disadvantage is the limitation of the size of the largest integer that a particular computer programming language can support.

Pseudo─code of a procedure for finding the integer square root of   X       (all variables are integers): Another version for the (above)   1st   perform   is:

Integer square roots of some values:

Compute and show all output here   (on this page)   for:

You can show more numbers for the 2nd requirement if the displays fits on one screen on Rosetta Code. If your computer programming language only supports smaller integers,   show what you can.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Isqrt (integer square root) of X step by step in the Ruby programming language

The provided Ruby code is a combination of three distinct parts:

  1. Commatize Refinement:

    • Extends the Integer class with a commatize method that formats numbers with commas as separators.
    • This refinement uses the gsub method to replace groups of three digits with a comma followed by the digits (e.g., 12345 becomes "12,345").
  2. Integer Square Root Approximation:

    • Defines the isqrt method, which approximates the square root of a given integer x.
    • It uses a bit shifting-based algorithm to iteratively refine an initial guess to obtain an approximation of the square root.
  3. Printing and Formatting:

    • Prints a sequence of square roots from 0 to 65, separated by spaces.
    • Prints the square root of 7 raised to powers from 1 to 73, in steps of 2, and formats the results using the commatize method to include commas as separators for large numbers.

Here's how the code works:

  1. The Commatize module defines a refinement to the Integer class, adding the commatize method.
  2. The using Commatize statement imports the refinement, allowing commatize to be used on integers.
  3. The isqrt method approximates the square root of an integer x.
  4. The first puts statement prints a sequence of square roots for numbers from 0 to 65.
  5. The second puts statement prints the square roots of 7 raised to powers from 1 to 73, in steps of 2. It uses the commatize method to format the results with commas for large numbers.

Overall, the code demonstrates how to refine existing classes in Ruby, how to approximate square roots efficiently, and how to format numbers with commas for readability.

Source code in the ruby programming language

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


  

You may also check:How to resolve the algorithm 15 puzzle solver step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Chaos game step by step in the Racket programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the SenseTalk programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Halt and catch fire step by step in the Z80 Assembly programming language