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{
""" 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)
put!(spigot,
return pairs, [
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 =
remwithnext = remain * 100 +
d = BigInt(findlast(x -> x * (divisor * 10 + x) <= remwithnext, 0:9) - 1)
remain = remwithnext - (divisor * 10 + d) * d
pushfirst!(founddigits, d)
put!(spigot,
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)
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)
function testspigotsqrt(arr)
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:
1.414213562373095048801688724209698078569671875376
94807317667973799073247846210703885038753432764157
27350138462309122970249248360558507372126441214970
99935831413222665927505592755799950501152782060571
47010955997160597027453459686201472851741864088919
86095523292304843087143214508397626036279952514079
89687253396546331808829640620615258352395054745750
28775996172983557522033753185701135437460340849884
71603868999706990048150305440277903164542478230684
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>
|