Integer roots: Difference between revisions

Added VisualBasic.Net version
m (Corrected decimal point for cube root and 7th root (by changing the input), left note about it.)
(Added VisualBasic.Net version)
Line 617:
14142135623730950488016887242096980[...]32952546758516447107578486024636008
</pre>
 
=={{header|Visual Basic .NET}}==
From the method described on [https://en.wikipedia.org/wiki/Nth_root_algorithm the Wikipedia page].<br>''Note:'' When using the Visual Studio IDE, don't forget to add a reference to '''System.Numerics'''.<br>Included is an Integer Square Root function to compare results to the Integer Nth Square root function.<br>Remember to choose the exponents carefully, otherwise one will obtain the digits of the nth root of 20, 200, 2000, etc..., instead of 2. For example, 4008 was chosen because it works for both ''n = 2'' and ''n = 3'', whereas 4004 was chosen for ''n = 7''<lang vbnet>Imports System
Imports System.Numerics
Imports Microsoft.VisualBasic.Strings
 
Public Module Module1
 
Public Function IntSqRoot(v As BigInteger) As BigInteger
Dim digs As Integer = Math.Max(0, v.ToString().Length / 2 - 1)
IntSqRoot = BigInteger.Parse("3" & StrDup(digs, "0"))
Dim term As BigInteger
Do
term = v / IntSqRoot
If Math.Abs(CDbl(term - IntSqRoot)) < 2 Then Exit Do
IntSqRoot = (IntSqRoot + term) / 2
Loop Until False
End Function
 
Public Function IntNthRoot(n As Integer, v As BigInteger) As BigInteger
Dim digs As Integer = Math.Max(0, v.ToString().Length / n - 1)
IntNthRoot = BigInteger.Parse(If(digs > 1, 3, 2).ToString() & StrDup(digs, "0"))
Dim va As BigInteger, dr As BigInteger
Do
va = v : For i As Integer = 2 To n : va /= IntNthRoot : Next
va -= IntNthRoot
dr = va / n : If dr = 0 Then Exit Do
IntNthRoot += dr
Loop Until False
End Function
 
Public Sub Main()
Dim b As BigInteger = BigInteger.Parse("2" & StrDup(4008, "0"))
Console.WriteLine("Integer Cube Root of 8:")
Console.WriteLine(IntNthRoot(3, 8).ToString()) ' given example
Console.WriteLine("Integer Cube Root of 9:")
Console.WriteLine(IntNthRoot(3, 9).ToString()) ' given example
Console.WriteLine("Integer Square Root of 2, (actually 2 * 10 ^ 4008, square root method):")
Console.WriteLine(IntSqRoot(b).ToString()) ' reality check
Console.WriteLine("Integer Square Root of 2, (actually 2 * 10 ^ 4008, nth root method):")
Console.WriteLine(IntNthRoot(2, b).ToString()) ' given example
Console.WriteLine("Integer Cube Root of 2, (actually 2 * 10 ^ 4008):")
Console.WriteLine(IntNthRoot(3, b).ToString()) ' bonus example
b /= 10000
Console.WriteLine("Integer 7th Root of 2, (actually 2 * 10 ^ 4004):")
Console.WriteLine(IntNthRoot(7, b).ToString()) ' bonus example
If Diagnostics.Debugger.IsAttached Then Console.Read()
End Sub
 
End Module
</lang>
{{out}}
<pre style="height:64ex;overflow:scroll">
Integer Cube Root of 8:
2
Integer Cube Root of 9:
2
Integer Square Root of 2, (actually 2 * 10 ^ 4008, square root method):
1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847160386899970699004815030544027790316454247823068492936918621580578463111596668713013015618568987237235288509264861249497715421833420428568606014682472077143585487415565706967765372022648544701585880162075847492265722600208558446652145839889394437092659180031138824646815708263010059485870400318648034219489727829064104507263688131373985525611732204024509122770022694112757362728049573810896750401836986836845072579936472906076299694138047565482372899718032680247442062926912485905218100445984215059112024944134172853147810580360337107730918286931471017111168391658172688941975871658215212822951848847208969463386289156288276595263514054226765323969461751129160240871551013515045538128756005263146801712740265396947024030051749531886292563138518816347800156936917688185237868405228783762938921430065586956868596459515550164472450983689603688732311438941557665104088391429233811320605243362948531704991577175622854974143899918802176243096520656421182731672625753959471725593463723863226148274262220867115583959992652117625269891754098815934864008345708518147223181420407042650905653233339843645786579679651926729239987536661721598257886026336361782749599421940377775368142621773879919455139723127406689832998989538672882285637869774966251996658352577619893932284534473569479496295216889148549253890475582883452609652409654288939453864662574492755638196441031697983306185201937938494005715633372054806854057586799967012137223947582142630658513221740883238294728761739364746783743196000159218880734785761725221186749042497736692920731109636972160893370866115673458533483329525467585164471075784860246360083444
Integer Square Root of 2, (actually 2 * 10 ^ 4008, nth root method):
1414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571470109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847160386899970699004815030544027790316454247823068492936918621580578463111596668713013015618568987237235288509264861249497715421833420428568606014682472077143585487415565706967765372022648544701585880162075847492265722600208558446652145839889394437092659180031138824646815708263010059485870400318648034219489727829064104507263688131373985525611732204024509122770022694112757362728049573810896750401836986836845072579936472906076299694138047565482372899718032680247442062926912485905218100445984215059112024944134172853147810580360337107730918286931471017111168391658172688941975871658215212822951848847208969463386289156288276595263514054226765323969461751129160240871551013515045538128756005263146801712740265396947024030051749531886292563138518816347800156936917688185237868405228783762938921430065586956868596459515550164472450983689603688732311438941557665104088391429233811320605243362948531704991577175622854974143899918802176243096520656421182731672625753959471725593463723863226148274262220867115583959992652117625269891754098815934864008345708518147223181420407042650905653233339843645786579679651926729239987536661721598257886026336361782749599421940377775368142621773879919455139723127406689832998989538672882285637869774966251996658352577619893932284534473569479496295216889148549253890475582883452609652409654288939453864662574492755638196441031697983306185201937938494005715633372054806854057586799967012137223947582142630658513221740883238294728761739364746783743196000159218880734785761725221186749042497736692920731109636972160893370866115673458533483329525467585164471075784860246360083445
Integer Cube Root of 2, (actually 2 * 10 ^ 4008):
12599210498948731647672106072782283505702514647015079800819751121552996765139594837293965624362550941543102560356156652593990240406137372284591103042693552469606426166250009774745265654803068671854055186892458725167641993737096950983827831613991551293136953661839474634485765703031190958959847411059811629070535908164780114735213254847712978802422085820532579725266622026690056656081994715628176405060664826773572670419486207621442965694205079319172441480920448232840127470321964282081201905714188996459998317503801888689594202055922021154729973848802607363697417887792157984675099539630078260959624203483238660139857363433909737126527995991969968377913168168154428850279651529278107679714002040605674803938561251718357006907984996341976291474044834540269715476228513178020643878047649322579052898467085805286258130005429388560720609747223040631357234936458406575916916916727060124402896700001069081035313852902700415084232336239889386496782194149838027072957176812879001445746227147702348357151905506722084818485009287239209282646606717174247753709737030012742918094054425696592075036357570375189603707473993461014490145157635960471111973845299132965726258904860978856180138677383615773009865983660805975756012787121486856242684556411651558179353228015896291299445004012084254141601575258416298814230973582153060405772425383645325335660
Integer 7th Root of 2, (actually 2 * 10 ^ 4004):
110408951367381233764950538762334472132532660078012416551453246414210632288038098071659828988630200514689715906557993125396921468043085579651064805838808196163919864392215583814551234397476339507890664685902921180613942144056283519219500774011043913929222338953790376732070503206390380988494445707084527925240582730725486467967183681658942999591682242459036160190261150569028438652686935172086652456800484770182207006433466758082204482396098451455092224240860882545144206285044829838431779372151867676523068340672781132725205233485925077681104722131036524174667129439905032</pre>
 
=={{header|zkl}}==