How to resolve the algorithm Isqrt (integer square root) of X step by step in the Go programming language
How to resolve the algorithm Isqrt (integer square root) of X step by step in the Go 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 Go programming language
. Overview of the Code: This Go program calculates the integer square roots of various numbers and presents the results in a formatted manner.
. Key Concepts:
- Big Integer (big.Int): The
big
package provides an arbitrary-precision integer type (big.Int
) that can represent integers of unlimited size. - Square Root: The square root of a number is another number that, when multiplied by itself, produces the original number.
. Main Function:
main
:- Executes the program.
- Prints integer square roots of numbers from 0 to 65 and of powers of 7 up to 7^73.
. Functions:
- isqrt:
- Computes the integer square root of a given non-negative
big.Int
numberx
. - Uses a binary search method to converge on the integer square root.
- Computes the integer square root of a given non-negative
- commatize:
- Formats a string by adding commas every three characters to improve readability of large numbers.
. Calculations and Output:
- The program calculates and prints the integer square roots of numbers from 0 to 65.
- It also calculates and prints the integer square roots of powers of 7 from 7^1 to 7^73. The output is formatted to align the power, the power of 7, and the integer square root.
. Example Output:
``` The integer square roots of integers from 0 to 65 are: 0 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 9 10 10 10 10 10 10 11 11 11 11 11 11 12 12 12 12 12 12 13 13 13 13 13 13 14 14 14 14 14 14 15 15 15
The integer square roots of powers of 7 from 7^1 up to 7^73 are:
power 3 7 ^ power integer square root
1 7 49 7 3 343 1,471 39 5 1,326 5,886 77 7 5,153 24,016 153 9 19,601 83,988 233 11 73,505 348,352 317 13 275,625 1,458,753 397 15 1,036,801 4,928,032 477 17 3,874,201 16,817,993 557 19 14,542,891 59,573,672 637 21 54,295,329 224,167,681 717 23 202,101,921 853,471,744 797 25 756,855,015 3,332,419,937 877 27 2,814,749,761 13,056,461,904 957 29 10,485,762,977 41,452,883,217 1037 31 39,100,263,169 161,061,273,696 1117 33 146,254,912,209 582,271,104,113 1197 35 547,561,516,225 2,181,736,125,744 1277 37 2,041,523,010,945 8,171,956,510,977 1357 39 7,599,796,076,673 29,303,275,625,744 1437 41 28,239,184,301,313 106,985,737,282,577 1517 43 104,956,737,205,217 396,540,551,341,088 1597 45 389,827,948,020,097 1,476,704,397,284,352 1677 47 1,442,310,602,481,025 5,453,665,191,754,249 1757 49 5,355,293,209,925,505 20,441,215,975,020,817 1837 51 19,986,577,839,730,225 75,930,399,680,077,248 1917 53 74,348,324,213,540,625 279,709,052,720,285,888 1997 55 275,573,195,600,962,505 1,034,210,550,801,062,912 2077 57 1,022,106,382,900,787,201 3,832,435,095,203,944,448 2157 59 3,797,069,744,100,634,081 14,406,812,382,815,777,856 2237 61 14,123,138,712,908,211,201 53,376,245,531,263,090,432 2317 63 52,474,411,385,016,632,321 197,529,806,245,052,473,856 2397 65 194,902,085,825,066,240,001 735,299,224,900,209,925,376 2477 67 724,184,347,250,264,426,241 2,716,760,533,600,799,749,632 2557 69 2,696,7
Source code in the go programming language
{% raw %}
package main
import (
"fmt"
"log"
"math/big"
)
var zero = big.NewInt(0)
var one = big.NewInt(1)
func isqrt(x *big.Int) *big.Int {
if x.Cmp(zero) < 0 {
log.Fatal("Argument cannot be negative.")
}
q := big.NewInt(1)
for q.Cmp(x) <= 0 {
q.Lsh(q, 2)
}
z := new(big.Int).Set(x)
r := big.NewInt(0)
for q.Cmp(one) > 0 {
q.Rsh(q, 2)
t := new(big.Int)
t.Add(t, z)
t.Sub(t, r)
t.Sub(t, q)
r.Rsh(r, 1)
if t.Cmp(zero) >= 0 {
z.Set(t)
r.Add(r, q)
}
}
return r
}
func commatize(s string) string {
le := len(s)
for i := le - 3; i >= 1; i -= 3 {
s = s[0:i] + "," + s[i:]
}
return s
}
func main() {
fmt.Println("The integer square roots of integers from 0 to 65 are:")
for i := int64(0); i <= 65; i++ {
fmt.Printf("%d ", isqrt(big.NewInt(i)))
}
fmt.Println()
fmt.Println("\nThe integer square roots of powers of 7 from 7^1 up to 7^73 are:\n")
fmt.Println("power 7 ^ power integer square root")
fmt.Println("----- --------------------------------------------------------------------------------- -----------------------------------------")
pow7 := big.NewInt(7)
bi49 := big.NewInt(49)
for i := 1; i <= 73; i += 2 {
fmt.Printf("%2d %84s %41s\n", i, commatize(pow7.String()), commatize(isqrt(pow7).String()))
pow7.Mul(pow7, bi49)
}
}
{% endraw %}
You may also check:How to resolve the algorithm Window creation step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Terminal control/Clear the screen step by step in the min programming language
You may also check:How to resolve the algorithm Fast Fourier transform step by step in the Run BASIC programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Box the compass step by step in the Gambas programming language