Square root by hand: Difference between revisions

m
syntax highlighting fixup automation
(added Arturo implementation)
m (syntax highlighting fixup automation)
Line 12:
=={{header|Arturo}}==
{{trans|Nim}}
<langsyntaxhighlight lang="rebol">i: 2
j: to :integer sqrt 2.0
k: new j
Line 37:
]
 
print ""</langsyntaxhighlight>
 
{{out}}
Line 46:
{{Trans|Visual Basic .NET}}
{{libheader|System.Numerics}}
<langsyntaxhighlight lang="csharp">using System;
using static System.Math;
using static System.Console;
Line 67:
if (n0 > 0) WriteLine("\nTime taken for {0} digits: {1}", n0, DateTime.Now - st); }
 
}</langsyntaxhighlight>{{out}}<pre style="height:32ex; overflow:scroll; white-space:pre-wrap;">14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Time taken for 500 digits: 00:00:00.0092331
</pre>
Line 73:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.bigint;
import std.math;
import std.stdio;
Line 100:
}
} while (n > 0);
}</langsyntaxhighlight>
{{out}}
<pre>14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Square Root of n 'By Hand' (n as bigint >= 1). Nigel Galloway: October 14th., 2020
let rec fN n g=match n/100I with i when i=0I->(n%100I)::g |i->fN i ((n%100I)::g)
Line 114:
 
sR 2I 1 480; sR 1089I 2 8
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 125:
{{trans|Visual Basic .NET}}
The original has been adjusted in a similar fashion to the Wren entry to deal with non-integer cases.
<langsyntaxhighlight lang="go">package main
 
import (
Line 198:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 219:
=={{header|FreeBASIC}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">' version 20-12-2020
' compile with: fbc -s console
 
Line 381:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre style="height:30ex; overflow:scroll; white-space:pre-wrap;">Square Root of 2 = 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008344491148185876555542864551233142199263113325179706084365597043528564100879185007603610091594656706768836055717400767569050961367194013249356052401859991050621081635977264313806054670102935699710424251057817495310572559349844511269227803449135066375687477602831628296055324224269575345290288387684464291732827708883180870253398523381227499908123718925407264753678503048215918018861671089728692292011975998807038185433325364602110822992792930728717807998880991767417741089830608003263118164279882311715436386966170299993416161487868601804550555398691311518601038637532500455818604480407502411951843056745336836136745973744239885532851793089603738989151731958741344288178421250219169518755934443873961893145499999061075870490902608835176362247497578588583680374579311573398020999866221869499225959132764236194105921003280261498745665996888740679561673918595728886424734635858868644968223860069833526427990562831656139139425576490620651860216472630333629750756978706066068564981600927187092921531323682813569889370974165044745909605374727965244770940992412387106144705439867436473384774548191008728862221495895295911878921491798339810837882781530655623158103606486758730360145022732088293513413872276841766784369052942869849083845574457940959862607424995491680285307739893829603621335398753205091998936075139064444957684569934712763645071632791547015977335486389394232572775400382602747856741725809514163071595978498180094435603793909855901682721540345815815210049366629534488271072923966023216382382666126268305025727811694510353793715688233659322978231929860646797898640920856095581426143636310046155943325504744939759339991254195323009321753044765339647066276116617535187546462096763455873861648801988484974792640450654448969100407942118169257968575637848814989864168549949163576144840470210339892153423770372333531156459443897036531667219490493518829058063074013468626416724701106534634939164071462855679801779338144240452691370666097776387848662380033923243704741153318725319060191659964553811578884138084332321053376746181217801429609283241136275254088737290512940733947943306194395693670207942951587822834932193166641113015495946983789776743444353933770995713498840789085081589236607008865810547094979046572298888089246128281601313370102908029099974564784958154561464871551639050241985790613109345878330620026220737247167668545549990499408571080992575992889323661543827195500578162513303815314657790792686850080698442847915242427544102680575632156532206188575122511306393702536292716196825125919202521605870118959673224423926742373449076464672737534796459881914980793171800242385545388603836831080077918246646275411744425001872777951816438345146346129902076334301796855438563166772351838933666704222211093914493028796381283988931173130843004212555018549850652945563776603146125590910461138476828235959247722862904264273616326458544339287726386034314980489639736332975488592568114929683612672589857383321643666348702347730261010613050729861153412994880877447311122954265275165366591173014236062652586907719821703709810464436047722673928298741525930695620638471082740821849067372330587430297092428994817392440786937528440104439904852087885191419354151290068173517030693869705900474251576552480784473621441050162008454441222559562029847259403528019067980680983003964539856859304586252606377974535599277472990648887454512424960763780108639001910580928747647207511092386059501954322816020887962151623385216128752285180252928761832570371728574067639449098254644221846543088066105802015847284067126302545937989065081685713716566859413005331970365964033766741461049563765103083661348931094780268129355733189055197052018451503996909866315251241161119259405528085649893195898345623319836834948808061715624391128663127978483719789533690152776005498055166350197855571101405552976338412750446860464766318326611651820675012047669910987219104447440326894364159594279219944235537187042995592403140917128481585438660053857135836398163094524075570093251682434416824083619792733728252154622469615332170268299509790890345948588783494396162043584224973971871139589273050921970549171769616004455808994278788803691694328945951472267229261248506961731638094108218600452861026965475763043102560271523139694821355198214097165490973199928349256740974903922971263486934145749331980417180761119639022786640759224341677624662362389131102703433045763681411283213263085822394562195980866129399962012341561763181743124200890149838485604808798646083935964923665142968125773143229145687168276219961182782695315749838026246517590541039761812876042163861345022132627277566124411336107751955577495086563606737866506231856406991228018757417854946612532759976979605977605907564891066610158384172028185304321190446577525542775437987260548817361982675816862832952607899322266836028385135122810593185910286415081570563197173151831362502435904146321223921766339826893682531505300598915470290953719326620734112349474336788469020139049784285216341442921458955828784766939464642678122190497856363552633682780518600986992489377860023987691698076566219438985443708059464333623338105874581623547560013659243524265714308346554576800237081467573252547025507476374716350678515991736937932510326827606286459146182047214863703707719269268236233347203792459646918105261391530862802914409654825638730927304265446629290458960637519187114693453619733247895727070315309309019211991999936157650035039840540674253879275279227247335667706078379113844889362613676570602636003151329520953952028548973844862561349244147086070866026763499787934208758361219471169942238484825959143045281070626015089691353030177200627170544020906695149152745977197059476954740952102878725578568800221937177435581107939308833845586482772910086295545661413067212308487402271210586863233882374138844289381554446471057556514684357029466350628938735698686883764803265195284146535173953027361201374203009867398385143219004360289826982935293994141292305803845650227072168151619410114498263013649008770483984883860906533685990545838952031856480414932721423908651649994316592079659535694307231129116292867975171566889054393220356912933245702080671944404973049439814082278296027994245410831666759214248351827238172050410392742888015562233807961475124335147310212845459448994449960007524375195701166834174474907958820995178367680232365176749723014874577427259947609621984327148352986111902728735849052179759083741974860267060537462315300393752123678677528486921958571375542696848278363178611099336801439159059748428580545161302301439790570161088986277796107506733326760486549292513997813905358822768937322049414839401355603565604421401761206051318068919899626061848318534018362378217266375804552471962661749254228528045714420485783421132280085287042054889923412785548123676153770710425446986852199112283542663499971274836607624624182073646661712839474847328047443040334410720042872712756702795675824292627194545805300266648996507956977817862194217200523716536946770419511191270462483605113028904643775114869488784961511884147191000125588383666067720841123515355881126778957155859041257626160106751315358021242733187100063582495450409957940725479890031682651237311905566829151943053708489307869197428290490386037231160992834243171222509945471501928666487871079519951800546338838443154817246354802445180308452734310006213710346257330600123497374435581809656784646415339051465691932456235314057791936989884236471835253758052577133112007971040683154926654020260468068183914378272147690632424695171286367384431398333711761594186999346626234537345235679401241680922911636095637216745283917099091466485073920515160560473787106154702169960746569309794426121469256159342564940191229895147325447151812632583688972822628332952403597007278633646045947071241747294687757059581573499628480995678392554742404489918870710696752425077452012293608105741426532347240641621410333533405511045212617503590284037454591864504727624342071770929793540102140964645028368341804075860810014072161924771798098596811154044644372856895928683197779778693464159846974513391774153790487788083002205833504674655532302858732583515708599649068672875967295038725475708791695547366917087012413339221484668517437066615488195293322727374360410825425966030398693265422350523691085951263008318467555034597583955058403567015588797773644380481821387070034402361804120021148372794227407873789331627081013626498289629272562445805397134142214511099995445821429237838810264839482339514187674689678318628681788272555825731939518155316951645014943572631060456949296709862520433938520782207622191003446926966334259085305816044978025776325448937080062677873179548529856683948694673356963001402931314190257807758169458152725293434225905197918316621644487517816967752767709130431573425640549229381873951108441668309249111597857733273638841418507379363002639218068001949823966647123131719025237031990587719774100071324075192041812214132425327294918600042008415485115474115730598721962129885416637208775224837694859747672933018683905225001486903826108482481981675931077727026488262090723847752905876504032667275848252185162310745449887588274656780949712308766144264148241579035703933122565189333562818361854057467063806183984894662842457365645642139072163052955293592848775552427545595133827715001784016553054854422850119883655756801593464505589944248496274127118698831580476918141567961853216571696452225945947124693199571164198618847977891211426811643837723848363186731860756477853699930387054663229698075675846821230280772610069691740782024794988210954733430112654544217019585237588078534800373724711876111000877190355388157319225133384249477450311881194745595365336609206419293440035078564223432923249297270847248235576717405895001268763600812452112448756434280946593133618564324148557807919311512650972958916052993030771056352454514834572092245519848905889042198065439733537575998248580375463927365376419674806269683827129200143495667485224724145486360362115847232317369980617199364211363145807119883968129570561158812462058857966505622150748208974776417708378705292420288029004400248068681254220757905942434704644895754402387369360474013086036075991743876156352967760580183349308796466270711608050737610718002215525191993796200709161383227280177313320190059780482079607580324994622385385803573478018713802840398120046812370790924572728576545104897170310237054867879336437815780740076774742152803118498155769816561516261157202045402644129931611707733125384612893676379183853705009420630609103254025847682220367682492794734000617751295263072656378530973686420007766665889932845661224650730022095628772726222780803954834038109628057649289746518436319498402612997618900467819092737096478278724357752206684654000246833074608783587655890530569425749909890392204630047145720590537120913142758865376931480400008717913845690993629987847885421778154073505170625320509514478220667252608620410799622270348081801380066100719226814029197683548842439916280980361859771935889226548587281632769054286174663230813628776499007377599324417521476776046936962233215175926450556452563840546700404521580075454379681038435585147943092296352197852283295745457271564793185041889607012805949229592183594937074580390321410436601637650955489445419026339119607411006694977802469540936562812753849632360106258465366705076517702969513039685870236791287541358806440263423823568060764074517611908833371209141576280565223790127356419353456526762962440266022824542619603422835240020503295053190853201496804513564334103431329223589697283108739569438131809431666913390526489148332879882762852563045120637614900045218642717111508976282752867146636117389828587425317216596247643323840034900496298789487001051884494118660439739107493757349528934770739638665933255438589993537994143840662422102268328511662511368344732896613210526750893794834463493035278532130127821152685942984375651745109303992495866460942386847002153550180378000187011113151937875401091495889080764733455002640980568321438116007514618278844904681248146893097430000109019843208666309225138112111599481279636783908122243781910187779940340765274060382341505327174162786748880857541012142866746631036108800181884354018236865322168775041197807652581153841736562183567501303445659593659097469007765630951563662834863997975493756384052967232835634030315916549588611222995999686827014284072391462300161735440831436438045892205541101795351355885271347984937876133791075655995414528917770157581348757680186242922229776662115422497113341739603196763909350512320947616642753474388333888699979916463836750324186324862841878469960963808275129963381739374220953475861632216305202703517037490298568525595814192954995176552582123473108197663301341750815123677523151607320881829564072634764505887576136189361870128904026792264704949678723740258130083476397564463263354967528574953701512710069446442062461753644289498604920521823213384326275335198829492086490709605921654573769095951389378997662628770868323505964098050188569819974056600544153213840734978154080943540759681561338964320840415315102432432476506355824097853468115105632389804013803814849735287444290693934437338180109010178859205640690769726390933911161368466669931806838234674038922367229255166024468597460763582903728529497158369063729095033685951182387240386566803844098591359996588300622799752913684945617051993291599492324340413727253807636840295187369817973536657095994220475105964407590707800203936232471823804137799283739673588096389547393847128950623044844732470443823390251314913859447521279271461067225268355552093101409825032410413568118889344170634801818879038524372843450413995267508390749293155948992799740206016686101060573836203436961399237050205913691224788144821970045564629606180152957267746654025224032152010626805924692941841465169269429703164474892255335681947010558607539503125748779271822019806805065513471892626509987040387239361526280911715016398391838208037107664447231125594297930841574857549712849567707689130531391519283160749372260464837412112410527404580769077499032167615319965660974300890284787922098945551403495667763293684189129282288893791392565790306170421951746426671762860073765482548949082367049041789827946948133710054375735229262593956809537167977177738428166119599319878150374402232529401665135964883989187712666676445928280724774198051183403577263019415056222709266888151008740810216304551119368970339875899163436766552459690022966390618234599224437161568188716785019552192690477008887628817012435907238421885909432302524008728363411346002474635054076317430285610328831446395259955777141624975159928860344101047533467745304372786885761196226858948138978843512251669067618791323446207724263898911175193575536755089771736080779854992933748575879407969489011853826051113623591734039139860900187224540287265129235072513463360399477972112534407969641965843292485838961537078625446240527341837296165871280996215675141677888852182017826857947508860561917614334505307242257944215044011899380328211769427535155005035938402619271248407353448057641513492090664332608718869318783911372491354290631432773141475652344276989264107291929647783152267653096337719078870212101736288928013320665538468875227098214169947453467839730618843380636885678875093483712812994594714167402106479446230475095969112132841857374507688021742000919037861149289993213698282550504394125234293878915292944880672904533715586858939119405867992679680197519294635313212046058273013652463549197477178431255147195610894481716873695950097551490905804237705507658316604552631788191592885801514109903351599927649260209167537965856540717214902727720720795330464094926792969801456474075861684175182703554191523285901319918975644427209195806647378539654749435033660984556942205412322091494769852266066869313494128460524360062619192009545595992992035766358447252088884387701098485096145536625056482223310827748771249645923940344103848804565572091537208369237042203903081669215344336555529659147737595207945959705914921302438333795709374716303640945224011982545503754397260803763665873652598952691167996010278358881115715841157447947403528689000948241339184513780599922518...</pre>
Line 387:
=={{header|Java}}==
{{trans|D}}
<langsyntaxhighlight lang="java">import java.math.BigInteger;
 
public class SquareRoot {
Line 418:
System.out.println();
}
}</langsyntaxhighlight>
{{out}}
<pre>14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372</pre>
Line 434:
 
'''Helper functions'''
<langsyntaxhighlight lang="jq"># Integer division
def idivide($j):
. as $i
Line 462:
else "isqrt requires a non-negative integer for accuracy" | error
end ;
</syntaxhighlight>
</lang>
'''sqrt'''
<langsyntaxhighlight lang="jq">def sqrt_by_hand($limit):
. as $n
| if $n < 0 then "sqrt_by_hand: input cannot be negative." | error
Line 514:
| "First \($digits[$i]) significant digits (at most) of the square root of \(.):",
sqrt_by_hand($digits[$i]),
""</langsyntaxhighlight>
{{out}}
<pre>
Line 535:
=={{header|Julia}}==
Uses channels to iterate the spigot flow.
<langsyntaxhighlight lang="julia">function sqrt_spigot(number::Integer, places=0, limit=10000, bufsize=32)
spigot = Channel{Char}(bufsize)
 
Line 658:
 
testspigotsqrt([2, 0.2, 0, 00.0001, 10.89, 144e-6, 2.0e4, 0.00000009, 1.44e+04, 1.44e-32])
</langsyntaxhighlight>{{out}}
<pre>
The square root of 2.0 is:
Line 783:
=={{header|Kotlin}}==
{{trans|java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
import kotlin.math.floor
import kotlin.math.sqrt
Line 816:
} while (n > 0)
println()
}</langsyntaxhighlight>
{{out}}
<pre>14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372</pre>
Line 823:
{{trans|Kotlin}}
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import math
import bignum
 
Line 845:
inc k, d
if n0 > 0: dec n
if n == 0: break</langsyntaxhighlight>
 
{{out}}
Line 852:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 897:
}
 
say "The square root of $_ is " . SpigotSqrt $_ for < 25 0.0625 152.2756 >;</langsyntaxhighlight>
{{out}}
<pre>The square root of 25 is 5
Line 906:
Based on https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Decimal_(base_10)<br>
The use of string inputs helps guarantee perfect accuracy.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"0.8.2"</span><span style="color: #0000FF;">)</span>
Line 997:
<span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0.0001"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"0.00000009"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"20000"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">99</span><span style="color: #0000FF;">},{</span><span style="color: #008000;">"2"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">500</span><span style="color: #0000FF;">}}</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">spigot_test</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<small>(the final "2" was re-joined up by hand)</small>
Line 1,020:
</pre>
stress test?:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.0"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_set_default_prec[ision] has been renamed)</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,032:
<span style="color: #000000;">rs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Pi (builtin) vs spigot_sqrt(pi) squared:\n %s\n %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ps</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rs</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,044:
{{trans|D}} (but with variable I, not constant 2)
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define (square-root-by-hand I digits-remaining)
Line 1,062:
(square-root-by-hand 2 1000)
(square-root-by-hand 256 100)
(square-root-by-hand 144 #f)</langsyntaxhighlight>
 
{{out}}
Line 1,072:
=={{header|Raku}}==
Implemented a [https://www.wikihow.com/Calculate-a-Square-Root-by-Hand#Finding-Square-Roots-Manually long division algorithm.].
<syntaxhighlight lang="raku" perl6line># 20201023 Raku programming solution
 
sub integral (Str $in) { # prepend '0' if length is odd
Line 1,130:
say "The square root of $_ is";
SpigotSqrt $_ ; print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>The square root of 25 is
Line 1,145:
 
It also handles the placing of a decimal point &nbsp; (if needed).
<langsyntaxhighlight lang="rexx">/*REXX program computes the square root by the old "by pen─n'─paper" (hand) method.*/
signal on halt /*handle the case of user interrupt. */
parse arg xx digs . /*obtain optional arguments from the CL*/
Line 1,181:
k= ? + k
end /*#*/
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,206:
Smalltalk has builtin arbitrary precision integer arithmetic.
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">|i j k d n n0 t|
i := 2.
j := 2 sqrt floor.
Line 1,233:
] doWhile:[n > 0].
].
Stdout print: e'\nTime taken for {n0} digits: {t}ms\n'.</langsyntaxhighlight>
{{out}}
<pre>Number of digits: 500
Line 1,247:
=={{header|Visual Basic .NET}}==
{{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.<langsyntaxhighlight lang="vbnet">Imports System.Math, System.Console, BI = System.Numerics.BigInteger
 
Module Module1
Line 1,267:
If n0 > 0 Then WriteLine (VbLf & "Time taken for {0} digits: {1}", n0, DateTime.Now - st)
End Sub
End Module</langsyntaxhighlight>{{out}}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:<pre style="height:32ex; overflow:scroll; white-space:pre-wrap;">14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Line 1,276:
{{libheader|math.big}}
The translation is clearer than the original thanks the infix operators of the math.big lib in Vlang
<langsyntaxhighlight lang="vlang">import math
import math.big
import strings
Line 1,347:
println(sqrt(n, digits[i]))
}
}</langsyntaxhighlight>
{{out}}With this version the result of sqrt(2) is erroneous from index 310. There is a problem in the math.big library which uses the tiny-bignum that hat limited capabilities.
=== Other version using v-gmp Module ===
This version gives the correct results
<langsyntaxhighlight lang="vlang">import math
import gmp
import strings
Line 1,416:
println(sqrt(n, digits[i]))
}
}</langsyntaxhighlight>
{{out}}<pre style="height:32ex; overflow:scroll; white-space:pre-wrap;>First 500 significant digits (at most) of the square root of 2:
1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372
Line 1,432:
{{libheader|Wren-big}}
The original has been adjusted to deal with any non-negative number, not just integers. Where appropriate a decimal point and leading zero(s) have been added but don't count towards the required number of digits. Trailing zeros do count but have been trimmed off for display purposes.
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt
 
var sqrt = Fn.new { |n, limit|
Line 1,485:
System.print()
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
10,327

edits