How to resolve the algorithm Isqrt (integer square root) of X step by step in the C# 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 C# 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 C# programming language

This code computes the integer square root of numbers 0 to 65 and integer square roots of odd powers of 7 up to the power of 73. The integer square root of a number is the largest integer whose square is less than or equal to the number.

The code uses the Newton-Raphson method to compute the integer square root. The method starts with an initial guess of 1 and then iteratively refines the guess until it converges to the correct answer. The isqrt function implements the Newton-Raphson method. It takes a BigInteger number as input and returns the integer square root of the number. The function uses a while loop to iterate until the guess converges to the correct answer. In each iteration, the function computes a new guess by using the following formula: guess = (guess + number / guess) / 2

The function terminates when the guess is equal to the previous guess.

The Main function first computes the integer square roots of numbers 0 to 65. It then computes the integer square roots of odd powers of 7 up to the power of 73. The function uses a for loop to iterate through the numbers. In each iteration, the function calls the isqrt function to compute the integer square root of the number. The function then prints the integer square root to the console.

Source code in the csharp programming language

using System;
using static System.Console;
using BI = System.Numerics.BigInteger;
 
class Program {
 
    static BI isqrt(BI x) { BI q = 1, r = 0, t; while (q <= x) q <<= 2; while (q > 1) {
        q >>= 2; t = x - r - q; r >>= 1; if (t >= 0) { x = t; r += q; } } return r; }
 
    static void Main() { const int max = 73, smax = 65;
        int power_width = ((BI.Pow(7, max).ToString().Length / 3) << 2) + 3,
            isqrt_width = (power_width + 1) >> 1;
        WriteLine("Integer square root for numbers 0 to {0}:", smax);
        for (int n = 0; n <= smax; ++n) Write("{0} ", 
            (n / 10).ToString().Replace("0", " ")); WriteLine();
        for (int n = 0; n <= smax; ++n) Write("{0} ", n % 10); WriteLine();
        WriteLine(new String('-', (smax << 1) + 1));
        for (int n = 0; n <= smax; ++n) Write("{0} ", isqrt(n));
        WriteLine("\n\nInteger square roots of odd powers of 7 from 1 to {0}:", max);
        string s = string.Format("[0,2] |[1,{0}:n0] |[2,{1}:n0]",
            power_width, isqrt_width).Replace("[", "{").Replace("]", "}");
        WriteLine(s, "n", "7 ^ n", "isqrt(7 ^ n)");
        WriteLine(new String('-', power_width + isqrt_width + 6));
        BI p = 7; for (int n = 1; n <= max; n += 2, p *= 49)
            WriteLine (s, n, p, isqrt(p)); }
}


  

You may also check:How to resolve the algorithm Find the missing permutation step by step in the R programming language
You may also check:How to resolve the algorithm Sort stability step by step in the OpenEdge/Progress programming language
You may also check:How to resolve the algorithm Search a list of records step by step in the Lingo programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the BASIC programming language
You may also check:How to resolve the algorithm 100 doors step by step in the Euphoria programming language