Square root by hand: Difference between revisions

add handling of non-integers and exponential notation
(→‎{{header|Wren}}: Adjusted to deal with non-integers.)
(add handling of non-integers and exponential notation)
Line 105:
=={{header|Julia}}==
Uses channels to iterate the spigot flow.
<lang julia>function sqrt_spigot(number::Integer, places=0, limit=10000, bufsize=32)
spigot = Channel{Int8Char}(bufsize)
 
evaldigits(arr) = evalpoly(BigInt(10), arr)
 
""" Mark off pairs of digits, starting from the decimal point, working left. """
Line 121 ⟶ 119:
push!(pairs, [d[i], d[i+1]])
end
places = length(pairs) - div(places , 2)
return pairs
end
Line 126 ⟶ 125:
""" look at first digit(s) and find largest i such that i^2 < that number """
function firststep!(pairs)
icurnum = evaldigitsevalpoly(BigInt(10), popfirst!(pairs))
firstdigiti = BigInt(findlast(x -> x * x <= icurnum, 0:9) - 1)
put!(spigot, Int8Char(firstdigit'0' + i))
return pairs, [firstdigiti], icurnum - firstdigiti * firstdigiti
end
 
"""
What is the largest number d that we can put in the units and also multiply times
the divisor such that the result is still be less than or equal to what we have?
"""
function nextstep!(pairs, founddigits, remain)
divisor = evaldigitsevalpoly(BigInt(10), founddigits) * 2
remwithnext = remain * 100 + evaldigitsevalpoly(BigInt(10), popfirst!(pairs))
d = BigInt(findlast(x -> x * (divisor * 10 + x) <= remwithnext, 0:9) - 1)
remain = remwithnext - (divisor * 10 + d) * d
pushfirst!(founddigits, d)
put!(spigot, Int8Char('0' + d))
return pairs, founddigits, remain
end
Line 149 ⟶ 148:
function longhand_sqrt(n)
p = markoff(n)
if places <= 0 # 0 <= n < 1, such as 0.00144
put!(spigot, '0')
put!(spigot, '.')
for i in places:1:-1
put!(spigot, '0')
end
end
pairs, founddigits, remain = firststep!(p)
for _ in 1:limit
Line 154 ⟶ 160:
push!(pairs, [0, 0], [0, 0], [0, 0], [0, 0])
end
(places -= 1) == 0 && put!(spigot, '.')
pairs, founddigits, remain = nextstep!(pairs, founddigits, remain)
end
Line 164 ⟶ 171:
end
 
function sqrt_spigot(str::String, lm=10000, bsiz=32)
const spigot = sqrt_spigot(2)
str = lowercase(str)
if occursin("e", str)
str, exdig = split(str, "e")
extra = parse(Int, exdig)
!occursin(".", str) && (str *= '.')
else
extra = 0
end
if occursin(".", str)
if str[1] == '.'
str = '0' * str
elseif str[end] == str
str = str * '0'
end
s1, s2 = split(str, ".")
if extra < 0 # negative exponent, so rewrite call in non-exponential form
pos = length(s1) + extra
if pos < 0
str = "0." * "0"^(-pos) * s1 * s2
else
str = s1[1:end-pos] * "." * s1[end-pos+1:end] * s2
end
return sqrt_spigot(str, lm, bsiz)
end
b1, b2, places = parse(BigInt, s1), parse(BigInt, s2), length(s2)
if extra > 0
b1 *= BigInt(10)^extra
b2 *= BigInt(10)^extra
end
if isodd(places)
n = b1 * BigInt(10)^(places + 1) + b2 * 10
places += 1
else
n = b1 * BigInt(10)^places + b2
end
return sqrt_spigot(n, places, lm, bsiz)
else
return sqrt_spigot(parse(BigInt, str), 0, lm, bsiz)
end
end
 
sqrt_spigot(number::Real; l=10000, b=32) = sqrt_spigot("$number", l, b)
for i in 1:500
 
print(take!(spigot))
function testspigotsqrt(arr)
i % 50 == 0 && println()
for num in arr
spigot = sqrt_spigot(num)
println("The square root of $num is:")
for i in 1:500
print(take!(spigot))
i % 50 == 0 && println()
end
println()
end
end
 
testspigotsqrt([2, 0.2, 0, 00.0001, 10.89, 144e-6, 2.0e4, 0.00000009, 1.44e+04, 1.44e-32])
</lang>{{out}}
<pre>
The square root of 2.0 is:
14142135623730950488016887242096980785696718753769
1.414213562373095048801688724209698078569671875376
48073176679737990732478462107038850387534327641572
94807317667973799073247846210703885038753432764157
73501384623091229702492483605585073721264412149709
27350138462309122970249248360558507372126441214970
99358314132226659275055927557999505011527820605714
99935831413222665927505592755799950501152782060571
70109559971605970274534596862014728517418640889198
47010955997160597027453459686201472851741864088919
60955232923048430871432145083976260362799525140798
86095523292304843087143214508397626036279952514079
96872533965463318088296406206152583523950547457502
89687253396546331808829640620615258352395054745750
87759961729835575220337531857011354374603408498847
28775996172983557522033753185701135437460340849884
16038689997069900481503054402779031645424782306849
71603868999706990048150305440277903164542478230684
29369186215805784631115966687130130156185689872372
92936918621580578463111596668713013015618568987237
 
The square root of 0.2 is:
0.447213595499957939281834733746255247088123671922
30514485417944908210418512756097988288288167575645
49939016352301547567008506535448894147727172720243
06690541773355634638375833162255329064527971316107
15227008350675700068467848281288841728650781945051
85254457752599034804881363223551817818996984742781
45945779696417728308537978819826338715403949735776
88501795082659123663538429999548496030608682300719
15336665024997630356278816001124841710487084471112
21261268564046818666396586791949270454240268349922
 
The square root of 0.0 is:
0.000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 0.0001 is:
0.010000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 10.89 is:
3.300000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 0.000144 is:
0.012000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 20000.0 is:
141.4213562373095048801688724209698078569671875376
94807317667973799073247846210703885038753432764157
27350138462309122970249248360558507372126441214970
99935831413222665927505592755799950501152782060571
47010955997160597027453459686201472851741864088919
86095523292304843087143214508397626036279952514079
89687253396546331808829640620615258352395054745750
28775996172983557522033753185701135437460340849884
71603868999706990048150305440277903164542478230684
92936918621580578463111596668713013015618568987237
 
The square root of 9.0e-8 is:
0.000300000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 14400.0 is:
120.0000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
 
The square root of 1.44e-32 is:
0.000000000000000120000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
</pre>
 
4,105

edits