Square root by hand: Difference between revisions
Content added Content deleted
(→{{header|Wren}}: Changed height of output box to accommodate new example.) |
(→{{header|Go}}: Adjusted to deal with non-integer cases.) |
||
Line 51: | Line 51: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Visual Basic .NET}} |
{{trans|Visual Basic .NET}} |
||
The original has been adjusted in a similar fashion to the Wren entry to deal with non-integer cases. |
|||
<lang go>package main |
<lang go>package main |
||
Line 63: | Line 64: | ||
var hundred = big.NewInt(100) |
var hundred = big.NewInt(100) |
||
func sqrt(n |
func sqrt(n float64, limit int) { |
||
if n < 0 { |
|||
log.Fatal("Number cannot be negative") |
|||
} |
|||
count := 0 |
|||
for n != math.Trunc(n) { |
|||
n *= 100 |
|||
count-- |
|||
} |
|||
i := big.NewInt(int64(n)) |
|||
j := new(big.Int).Sqrt(i) |
j := new(big.Int).Sqrt(i) |
||
count += len(j.String()) |
|||
k := new(big.Int).Set(j) |
k := new(big.Int).Set(j) |
||
d := new(big.Int).Set(j) |
d := new(big.Int).Set(j) |
||
t := new(big.Int) |
t := new(big.Int) |
||
digits := 0 |
digits := 0 |
||
var sb strings.Builder |
|||
for digits < limit { |
for digits < limit { |
||
sb.WriteString(d.String()) |
|||
t.Mul(k, d) |
t.Mul(k, d) |
||
i.Sub(i, t) |
i.Sub(i, t) |
||
Line 91: | Line 102: | ||
digits = digits + 1 |
digits = digits + 1 |
||
} |
} |
||
root := strings.TrimRight(sb.String(), "0") |
|||
⚫ | |||
if len(root) == 0 { |
|||
root = "0" |
|||
} |
|||
if count > 0 { |
|||
root = root[0:count] + "." + root[count:] |
|||
} else if count == 0 { |
|||
root = "0." + root |
|||
} else { |
|||
root = "0." + strings.Repeat("0", -count) + root |
|||
} |
|||
root = strings.TrimSuffix(root, ".") |
|||
⚫ | |||
} |
} |
||
func main() { |
func main() { |
||
numbers := []float64{2, 0.2, 10.89, 625, 0.0001} |
|||
sqrt(2, 480) // enough for demo purposes |
|||
digits := []int{500, 80, 8, 8, 8} |
|||
for i, n := range numbers { |
|||
fmt.Printf("First %d significant digits (at most) of the square root of %g:\n", digits[i], n) |
|||
sqrt(n, digits[i]) |
|||
fmt.Println() |
|||
} |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
<pre style="height: |
<pre style="height:72ex; overflow:scroll; white-space:pre-wrap;"> |
||
First 500 significant digits (at most) of the square root of 2: |
|||
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871 |
|||
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372 |
|||
First 80 significant digits (at most) of the square root of 0.2: |
|||
0.44721359549995793928183473374625524708812367192230514485417944908210418512756097 |
|||
First 8 significant digits (at most) of the square root of 10.89: |
|||
3.3 |
|||
First 8 significant digits (at most) of the square root of 625: |
|||
25 |
|||
First 8 significant digits (at most) of the square root of 0.0001: |
|||
0.01 |
|||
</pre> |
</pre> |
||