Square root by hand: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎Visual Basic .NET: added command line parameter for limiting output to a number of digits)
Line 6: Line 6:
== Visual Basic .NET ==
== Visual Basic .NET ==
{{libheader|System.Numerics}}
{{libheader|System.Numerics}}
This is "spigot like", but not a true spigot, just an implementation of the "by hand" method of computing the square root, in this case, of two.<lang vbnet>Imports System.Math
This is "spigot like", but not a true spigot, just an implementation of the "by hand" method of computing the square root, in this case, of two.<lang vbnet>Imports System.Math, System.Console, BI = System.Numerics.BigInteger
Imports System.Console
Imports BI = System.Numerics.BigInteger


Module Module1
Module Module1
Line 15: Line 13:
Dim i, j, k, d As BI : i = 2
Dim i, j, k, d As BI : i = 2
j = CType(Floor(Sqrt(CDbl(i))), BI) : k = j : d = j
j = CType(Floor(Sqrt(CDbl(i))), BI) : k = j : d = j
Dim n As Integer = -1, n0 As Integer = -1,
st As DateTime = DateTime.Now
If args.Length > 0 Then Integer.TryParse(args(0), n)
If n > 0 Then n0 = n Else n = 1
Do
Do
Write(d)
Write(d) : i = (i - k * d) * 100 : k = 20 * j
i = (i - k * d) * 100 : k = 20 * j
For d = 1 To 10
For d = 1 To 10
If (k + d) * d > i Then d -= 1 : Exit For
If (k + d) * d > i Then d -= 1 : Exit For
Next
Next
j = j * 10 + d : k += d
j = j * 10 + d : k += d : If n0 > 0 Then n = n - 1
Loop While True
Loop While n > 0
If n0 > 0 Then WriteLine (VbLf & "Time taken for {0} digits: {1}", n0, DateTime.Now - st)
End Sub
End Sub
End Module</lang>{{out}}Execute without any command line parameters for it to run until it crashes (due to BigInteger variables eating up available memory).
End Module</lang>{{out}}showing only the first few digits<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372352885092648612494977154218334204285686060146824720771435854874155657069677653720226485447015858801620758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256117322040245091227700226941127573627280495738108967504018369868368450725799364729060762996941380475654823728997180326802474420629269124859052181004459842150591120249441341728531478105803603371077309182869314710171111683916581726889419758716582152128229518488472089694633862891562882765952635140542267653239694617511291602408715510135150455381287560052631468017127402653969470240300517495318862925631385188163478001569369176881852378684052287837629389214300655869568685964595155501644724509836896036887323114389415576651040883914292338113206052433629485317049915771756228549741438999188021762430965206564211827316726257539594717255934637238632261482742622208671155839599926521176252698917540988159348640083457085181472231814204070426509056532333398436457865796796519267292399875366617215982578860263363617827495994219403777753681426217738799194551397231274066898329989895386728822856378697749662519966583525776198939322845344735694794962952168891485492538904755828834526096524096542889394538646625744927556381964410316979833061852019379384940057156333720548068540575867999670121372239475821426306585132217408832382947287617393647467837431960001592188807347857617252211867490424977366929207311096369721608933708661156734585334833295254675851644710757848602463600834449114818587655554286455123314219926311332517970608436559704352856410087918500760361009159465670676883605571740076756905096136719401324935605240185999105062108163597726431380605467010293569971042425105781749531057255934984451126922780344913506637568747760283162829605532422426957534529028838768446429173282770888318087025339852338122749990812371892540726475367850304821591801886167108972869229201197599880703818543332536460211082299279293072871780799888099176741774108983060800326311816427988231171543638696617029999341616148786860180455055539869131151860103863753250045581860448040750241195184305674</pre>

Output with command line parameter of 500:<pre style="height:64ex; overflow:scroll; white-space:pre-wrap;">14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Time taken for 500 digits: 00:00:00.0263710</pre>


=={{header|Wren}}==
=={{header|Wren}}==

Revision as of 19:51, 12 October 2020

Square root by hand is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Create a program that will calculate n digits of the square root of a number.

The program should continue forever (or until the number of digits is specified) calculating and outputting each decimal digit in succession. The program should be a "spigot algorithm" generating the digits of the number sequentially from left to right providing increasing precision as the algorithm proceeds.

Visual Basic .NET

This is "spigot like", but not a true spigot, just an implementation of the "by hand" method of computing the square root, in this case, of two.<lang vbnet>Imports System.Math, System.Console, BI = System.Numerics.BigInteger

Module Module1

   Sub Main(ByVal args As String())
       Dim i, j, k, d As BI : i = 2
       j = CType(Floor(Sqrt(CDbl(i))), BI) : k = j : d = j
       Dim n As Integer = -1, n0 As Integer = -1,
           st As DateTime = DateTime.Now
       If args.Length > 0 Then Integer.TryParse(args(0), n)
       If n > 0 Then n0 = n Else n = 1
       Do
           Write(d) : i = (i - k * d) * 100 : k = 20 * j
           For d = 1 To 10
               If (k + d) * d > i Then d -= 1 : Exit For
           Next
           j = j * 10 + d : k += d : If n0 > 0 Then n = n - 1
       Loop While n > 0
       If n0 > 0 Then WriteLine (VbLf & "Time taken for {0} digits: {1}", n0, DateTime.Now - st)
   End Sub

End Module</lang>

Output:

Execute without any command line parameters for it to run until it crashes (due to BigInteger variables eating up available memory). Output with command line parameter of 500:

14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Time taken for 500 digits: 00:00:00.0263710

Wren

Translation of: Visual Basic .NET
Library: Wren-big

<lang ecmascript>import "/big" for BigInt

var sqrt = Fn.new { |n, limit|

   var i = BigInt.new(n)
   var j = i.isqrt
   var k = j
   var d = j
   var digits = 0
   while (digits < limit) {
       System.write(d)
       i = (i - k*d) * 100
       k = j * 20
       d = BigInt.one
       while (d <= 10) {
           if ((k + d)*d > i) {
               d = d.dec
               break
           }
           d = d.inc
       }
       j = j*10 + d
       k = k + d
       digits = digits + 1
   }
   System.print()

}

sqrt.call(2, 480) // enough for demo purposes</lang>

Output:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871