Square root by hand: Difference between revisions

Vlang initial version
(Vlang initial version)
Line 1,090:
Output with command line parameter of 500:<pre style="height:32ex; overflow:scroll; white-space:pre-wrap;">14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Time taken for 500 digits: 00:00:00.0263710</pre>
 
=={{header|Vlang}}==
{{trans|Go}}
{{libheader|math.big}}
The translation is clearer than the original thanks the infix operators of the math.big lib in Vlang
<lang vlang>import math
import math.big
import strings
fn sqrt(n f64, limit int) string {
one := big.from_int(1)
ten := big.from_int(10)
twenty := big.from_int(20)
hundred := big.from_int(100)
mut n0 := n
if n0 < 0.0 {
panic("Number cannot be negative")
}
mut count := 0
for n0 != math.trunc(n0) {
n0 *= 100
count--
}
mut i := big.from_int(int(n0))
mut j := i.isqrt()
count += j.str().len
mut k := j.clone()
mut d := j.clone()
mut digits := 0
mut sb := ""
for digits < limit {
sb += d.str()
i = (i - k*d) * hundred
k = j * twenty
d = one
for big.cmp(d, ten) <= 0 {
if big.cmp((k + d)*d, i) > 0 {
d.dec()
break
}
d.inc()
}
j = j*ten + d
k = k + d
digits++
}
 
mut root := sb.trim_right("0")
if root.len == 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 = root.trim_suffix(".")
if root.len > limit && root.contains(".") {
l := root.after_char(`.`)
if l.len > limit {
root = root[0..(root.len -(l.len - limit))]
}
}
return root
}
fn main() {
numbers := [f64(2), 0.2, 10.89, 625, 0.0001]
digits := [500, 80, 8, 8, 8]
for i, n in numbers {
println("First ${digits[i]} significant digits (at most) of the square root of $n:")
println(sqrt(n, digits[i]))
}
}</lang>
{{out}}From index 310 the result is erroneous. There must be a problem in the math.big library.
<pre style="height:72ex; overflow:scroll; white-space:pre-wrap;">First 500 significant digits (at most) of the square root of 2:
1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725330011005640000200000003001000111402000702610001020010001611001100000200040007102100104111100076006141122001100401000138002000011012220010401152211110711200024000111111511211110000101201101111210
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>
 
=={{header|Wren}}==
Anonymous user