Integer roots: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 404: Line 404:


func root(n, x)
func root(n, x)
for nr = x to 1 step -1
for nr = floor(sqrt(x)) to 1 step -1
if pow(nr, n) <= x
if pow(nr, n) <= x
see nr + nl
see nr + nl

Revision as of 07:13, 9 October 2017

Integer roots 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.
Task

Create a program that computes an approximation of the principal   Nth   root of   X   as the largest integer less than or equal to   R   for which   RN=X.

──where:

       N  is a positive integer. 
       X  is a non-negative integer. 
       R  (the root)   is a non-negative real number. 

No arbitrary limits should be placed on the magnitudes of the numbers involved.


Example:   With   N=3   and   X=8   you would calculate the number   2   because  

Example:   With   N=3   and   X=9  you would again calculate the number   2   because 2 is the largest integer less than or equal to the root   R.

Example:   With   N=2   and   X=2×1002,000   you would calculate a large integer consisting of the first   2,001   digits (in order) of the square root of two.

Elixir

Translation of: Ruby

<lang elixir>defmodule Integer_roots do

 def root(_, b) when b<2, do: b
 def root(a, b) do
   a1 = a - 1
   f = fn x -> (a1 * x + div(b, power(x, a1))) |> div(a) end
   c = 1
   d = f.(c)
   e = f.(d)
   until(c, d, e, f)
 end
 
 defp until(c, d, e, _) when c in [d, e], do: min(d, e)
 defp until(_, d, e, f), do: until(d, e, f.(e), f)
 
 defp power(_, 0), do: 1
 defp power(n, m), do: Enum.reduce(1..m, 1, fn _,acc -> acc*n end)
 
 def task do
   IO.puts root(3,8)
   IO.puts root(3,9)
   IO.puts "First 2,001 digits of the square root of two:"
   IO.puts root(2, 2 * power(100, 2000))
 end

end

Integer_roots.task</lang>

Output:
2
2
First 2,001 digits of the square root of two:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

Go

int

<lang go>package main

import "fmt"

func main() {

   fmt.Println(root(3, 8))
   fmt.Println(root(3, 9))
   fmt.Println(root(2, 2e18))

}

func root(N, X int) int {

   // adapted from https://en.wikipedia.org/wiki/Nth_root_algorithm
   for r := 1; ; {
       x := X
       for i := 1; i < N; i++ {
           x /= r
       }
       x -= r
       // A small complication here is that Go performs truncated integer
       // division but for negative values of x, Δr in the line below needs 
       // to be computed as the floor of x / N.  The following % test and
       // correction completes the floor division operation (for positive N.)
       Δr := x / N
       if x%N < 0 {
           Δr--
       }
       if Δr == 0 {
           return r
       }
       r += Δr
   }

}</lang>

Output:
2
2
1414213562

big.Int

<lang go>package main

import (

   "fmt"
   "math/big"

)

func main() {

   fmt.Println(root(3, "8"))
   fmt.Println(root(3, "9"))
   fmt.Println(root(2, "2000000000000000000"))
   fmt.Println(root(2, "200000000000000000000000000000000000000000000000000"))

}

var one = big.NewInt(1)

func root(N int, X string) *big.Int {

   var xx, x, Δr big.Int
   xx.SetString(X, 10)
   nn := big.NewInt(int64(N))
   for r := big.NewInt(1); ; {
       x.Set(&xx)
       for i := 1; i < N; i++ {
           x.Quo(&x, r)
       }
       // big.Quo performs Go-like truncated division and would allow direct
       // translation of the int-based solution, but package big also provides
       // Div which performs Euclidean rather than truncated division.
       // This gives the desired result for negative x so the int-based
       // correction is no longer needed and the code here can more directly
       // follow the Wikipedia article.
       Δr.Div(x.Sub(&x, r), nn)
       if len(Δr.Bits()) == 0 {
           return r
       }
       r.Add(r, &Δr)
   }

}</lang>

Output:
2
2
1414213562
14142135623730950488016887

Haskell

Translation of: Python

<lang haskell>root :: Integer -> Integer -> Integer root a b = findAns sequence where

 sequence = iterate (\x -> (a1 * x + b `div` (x ^ a1)) `div` a) 1
 a1 = a - 1
 findAns (x:xs@(y:z:_)) | x == y || x == z = min y z
                        | otherwise        = findAns xs

main :: IO () main = do

 print $ root 3 8
 print $ root 3 9
 print $ root 2 (2 * 100^2000) -- first 2001 digits of the square root of 2</lang>
Output:
2
2
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

J

<.@%: satisfies this task. Left argument is the task's N, right argument is the task's X:

<lang J> 9!:37]0 4096 0 222 NB. set display truncation sufficiently high for our results

  2 <.@%: (2*10x^2*2000)

141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

  3 <.@%: (2*10x^2*2000)

27144176165949065715180894696794892048051077694890969572843654428033085563287658494871973768515010449601702702662017016622108188038292129512829222732037939681464769491319263029308919709511736401200395299672806902057959507281705818417585572775465293620106435558459837272246448049135012971629241921717289904494332635356114519208640365765906522454723182775121756558058020787429240528065700321862315922465987881667832001482693220149093231249941256750252873117504822276540899360702266289427386749058832442643990936924594623694605667125995688788028079451303313515777223983018552490248388121970980055977541748894293734175182220013380497630428176870053423294103392285168797917553010332228664978678396929617114885278335650885524410898341213271192520021355449870508579216359067962061031950345530646092202370608763454397416764433915183368398263533906772869972563479248093751375796381425079119097628053496428734814910307755317031117606073779997125797512066497555354285360734633889394275558674944424368960732987910929093583629174893939036518727793282632439102479840614327136348027409016670160346303867705846755103908964945780837562103026771901489757443287280572195601219016859180373403783498753667545621963282035797597576337893795984255961467481252116653755272803423453851317757500585155874395469445425245653837328715044666730082806623655698726925

  5 <.@%: (2*10x^2*2000)

114869835499703500679862694677792758944385088909779750551371111849360320625351305681147311301150847391457571782825280872990018972855371267615994917020637676959403854539263226492033301322122190625130645468320078386350285806907949085127708283982797043969640382563667945344431106523789654147255972578315704103326302050272017414235255993151553782375173884359786924137881735354092890268530342009402133755822717151679559278360263800840317501093689917495888199116488588871447782240220513546797235647742625493141141704109917646404017146978939243424915943739448283626010758721504375406023613552985026793701507511351368254645700768390780390334017990233124030682358360249760098999315658413563173197024899154512108923313999675829872581317721346549115423634135836394159076400636688679216398175376716152621781331348

  7 <.@%: (2*10x^2*2000)

29619362959451736245702628695019269518064618216015009169507699742781423769947484925822512257735101524178182602734424986961003971858127002794053824818478879396020132662403256874761276690431037137165264232256601651438511207764019815767975124455844526943932927494896013055497926678521360177960529077012650088983239249505488961115547364229473827474458408002500739618874659540108997885564940730803150961523774615079827002013042942440654069714159530336055547627964891459096727426898214883744931710925020592035759639587602673656267343846153343265577563529779031634608306646526796</lang>

Kotlin

Translation of: Python

<lang scala>// version 1.1.2

import java.math.BigInteger

val bigZero = BigInteger.ZERO val bigOne = BigInteger.ONE val bigTwo = BigInteger.valueOf(2L)

fun BigInteger.iRoot(n: Int): BigInteger {

   require(this >= bigZero && n > 0)
   if (this < bigTwo) return this
   val n1 = n - 1
   val n2 = BigInteger.valueOf(n.toLong())
   val n3 = BigInteger.valueOf(n1.toLong())
   var c = bigOne
   var d = (n3 + this) / n2
   var e = (n3 * d + this / d.pow(n1)) / n2
   while (c != d && c != e) {
       c = d
       d = e
       e = (n3 * e + this / e.pow(n1)) / n2
   }
   return if (d < e) d else e

}

fun main(args: Array<String>) {

   var b: BigInteger
   b = BigInteger.valueOf(8L)
   println("3rd integer root of 8 = ${b.iRoot(3)}\n")
   b = BigInteger.valueOf(9L)
   println("3rd integer root of 9 = ${b.iRoot(3)}\n")    
   b = BigInteger.valueOf(100L).pow(2000) * bigTwo
   println("First 2001 digits of the square root of 2:")
   println(b.iRoot(2))

}</lang>

Output:
3rd integer root of 8 = 2

3rd integer root of 9 = 2

First 2001 digits of the square root of 2:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

Perl 6

Translation of: Python

<lang perl6>sub integer_root ( Int $p where * >= 2, Int $n --> Int ) {

   my Int $d = $p - 1;
   my $guess = '1' ~ ( '0' x ($n.chars / $p) );
   my $iterator = { ( $d * $^x   +   $n div ($^x ** $d) ) div $p };
   my $endpoint = {      $^x      ** $p <= $n
                    and ($^x + 1) ** $p >  $n };
   return [min] (+$guess, $iterator ... $endpoint)[*-1, *-2];

}

say integer_root( 2, 2 * 100 ** 2000 );</lang>

Output:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

Python

<lang python>def root(a,b):

   if b<2:return b
   a1=a-1
   c=1
   d=(a1*c+b//(c**a1))//a
   e=(a1*d+b//(d**a1))//a
   while c!=d and c!=e:
       c,d,e=d,e,(a1*e+b//(e**a1))//a
   return min(d,e)

print("First 2,001 digits of the square root of two:\n{}".format(root(2,2*100**2000)))</lang>

Output:
First 2,001 digits of the square root of two:
141421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157273501384623091229702492483605585073721264412149709993583141322266592750559275579995050115278206057147010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884716038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016207584749226572260020855844665214583988939443709265918003113882464681570826301005948587040031864803421948972782906410450726368813137398552561173220402450912277002269411275736272804957381089675040183698683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884720896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558695686859645951555016447245098368960368873231143894155766510408839142923381132060524336294853170499157717562285497414389991880217624309652065642118273167262575395947172559346372386322614827426222086711558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668983299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685405758679996701213722394758214263065851322174088323829472876173936474678374319600015921888073478576172522118674904249773669292073110963697216089337086611567345853348332952546758516447107578486024636008

Racket

See #Scheme, there’s very little can be done to improve it.

REXX

No error checking is performed to ensure the root is a non-zero integer.

This version incorporates some optimization when computing square roots   (because   M   is unity,   there is no need to
multiply the guess [G] by unity,   and no need to compute the guess to the 1st power,   bypassing some trivial arithmetic).

integer result only

<lang rexx>/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/ parse arg num root digs . /*obtain the optional arguments from CL*/ if num== | num=="," then num= 2 /*Not specified? Then use the default.*/ if root== | root=="," then root= 2 /* " " " " " " */ if digs== | digs=="," then digs=2001 /* " " " " " " */ numeric digits digs /*utilize this number of decimal digits*/ say 'number=' num /*display the number that will be used.*/ say ' root=' root /* " " root " " " " */ say 'digits=' digs /* " dec. digits " " " " */ say /* " a blank line. */ say 'result:'; say rootI(num, root, digs) /* " what it is; display the root.*/ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ rootI: procedure; parse arg x,root,p /*obtain the numbers, Y is the root #.*/

      numeric digits p*root+length(x)           /*double the number of digits  + guard.*/
      if x<2  then return x                     /*B is one or zero?  Return that value.*/
      z=x*(10**root)**p                         /*calculate the number with appended 0s*/
      m=root - 1                                /*utilize a diminished (by one) power. */
      g=(1 + z) % root                          /*take a stab at the first root guess. */
      old=.                                     /* [↓]  When M=1, a fast path for sqrt.*/
      if m==1  then  do  until old==g;   old=g;     g=(g   + z %  g     )  % root;    end
               else  do  until old==g;   old=g;     g=(g*m + z % (g**m) )  % root;    end
      return left(g,p)                          /*return the  Nth root of Z to invoker.*/</lang>

output   when the defaults are being used:

number= 2
  root= 2
digits= 2001

result:
14142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714
70109559971605970274534596862014728517418640889198609552329230484308714321450839762603627995251407989687253396546331808829640620615258352395054745750287759961729835575220337531857011354374603408498847
16038689997069900481503054402779031645424782306849293691862158057846311159666871301301561856898723723528850926486124949771542183342042856860601468247207714358548741556570696776537202264854470158588016
20758474922657226002085584466521458398893944370926591800311388246468157082630100594858704003186480342194897278290641045072636881313739855256117322040245091227700226941127573627280495738108967504018369
86836845072579936472906076299694138047565482372899718032680247442062926912485905218100445984215059112024944134172853147810580360337107730918286931471017111168391658172688941975871658215212822951848847
20896946338628915628827659526351405422676532396946175112916024087155101351504553812875600526314680171274026539694702403005174953188629256313851881634780015693691768818523786840522878376293892143006558
69568685964595155501644724509836896036887323114389415576651040883914292338113206052433629485317049915771756228549741438999188021762430965206564211827316726257539594717255934637238632261482742622208671
15583959992652117625269891754098815934864008345708518147223181420407042650905653233339843645786579679651926729239987536661721598257886026336361782749599421940377775368142621773879919455139723127406689
83299898953867288228563786977496625199665835257761989393228453447356947949629521688914854925389047558288345260965240965428893945386466257449275563819644103169798330618520193793849400571563337205480685
40575867999670121372239475821426306585132217408832382947287617393647467837431960001592188807347857617252211867490424977366929207311096369721608933708661156734585334833295254675851644710757848602463600
8

true results


Negative and complex roots are supported.   The expressed root may have a decimal point. <lang rexx>/*REXX program calculates the Nth root of a number to a specified number of decimal digs*/ parse arg num root digs . /*obtain the optional arguments from CL*/ if num== | num=="," then num= 2 /*Not specified? Then use the default.*/ if root== | root=="," then root= 2 /* " " " " " " */ if digs== | digs=="," then digs=2001 /* " " " " " " */ numeric digits digs /*utilize this number of decimal digits*/ say 'number=' num /*display the number that will be used.*/ say ' root=' root /* " " root " " " " */ say 'digits=' digs /* " dec. digits " " " " */ say /* " a blank line. */ say 'result:'; say iRoot(num, root) /* " what it is; display the root.*/ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ iRoot: procedure; parse arg x 1 ox, y 1 oy /*obtain the numbers, Y is the root #.*/ i=; x=abs(x); y=abs(y) /*use the absolute values of X and Y. */ if ox<0 & oy//2==0 then do; i='i'; ox=x; end /*if the results will be imaginary ··· */ od=digits() /*the current number of decimal digits.*/ a=od+9 /*bump the decimal digits by nine. */ numeric form /*number will be in exponential form.*/ parse value format(x,2,1,,0) 'E0' with ? 'E' _ . /*obtain exponent so we can do division*/ g=(?/y'E'_ % y) + (x>1) /*this is a best first guess of a root.*/ m=y-1 /*define a (fast) variable for later. */ d=5 /*start with only five decimal digits. */

            do  until d==a                      /*keep computing 'til we're at max digs*/
            d=min(d+d,a);           dm=d-2      /*bump number of (growing) decimal digs*/
            numeric digits d                    /*increase the number of decimal digits*/
            o=0                                 /*set the old value to zero (1st time).*/
                do  until o=g;      o=g         /*keep computing as long as  G changes.*/
                g=format((m*g**y+x)/y/g**m,,dm) /*compute the  Yth  root of  X.        */
                end   /*until o=g*/
            end       /*until d==a*/

_=g*sign(ox) /*change the sign of the result, maybe.*/ numeric digits od /*set numeric digits to the original.*/ if oy<0 then return (1/_)i /*Is the root negative? Use reciprocal*/

             return (_/1)i                      /*return the  Yth root of X to invoker.*/</lang>

output   when the defaults are being used:

number= 2
  root= 2
digits= 2001

result:
1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572735013846230912297024924836055850737212644121497099935831413222665927505592755799950501152782060571
47010955997160597027453459686201472851741864088919860955232923048430871432145083976260362799525140798968725339654633180882964062061525835239505474575028775996172983557522033753185701135437460340849884
71603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372352885092648612494977154218334204285686060146824720771435854874155657069677653720226485447015858801
62075847492265722600208558446652145839889394437092659180031138824646815708263010059485870400318648034219489727829064104507263688131373985525611732204024509122770022694112757362728049573810896750401836
98683684507257993647290607629969413804756548237289971803268024744206292691248590521810044598421505911202494413417285314781058036033710773091828693147101711116839165817268894197587165821521282295184884
72089694633862891562882765952635140542267653239694617511291602408715510135150455381287560052631468017127402653969470240300517495318862925631385188163478001569369176881852378684052287837629389214300655
86956868596459515550164472450983689603688732311438941557665104088391429233811320605243362948531704991577175622854974143899918802176243096520656421182731672625753959471725593463723863226148274262220867
11558395999265211762526989175409881593486400834570851814722318142040704265090565323333984364578657967965192672923998753666172159825788602633636178274959942194037777536814262177387991945513972312740668
98329989895386728822856378697749662519966583525776198939322845344735694794962952168891485492538904755828834526096524096542889394538646625744927556381964410316979833061852019379384940057156333720548068
54057586799967012137223947582142630658513221740883238294728761739364746783743196000159218880734785761725221186749042497736692920731109636972160893370866115673458533483329525467585164471075784860246360
08

output   when using the input of:   -81

number= -81
  root= 2
digits= 2001

result:
9i

output   when using the input of:   4   -2

number= 4
  root= -2
digits= 2001

result:
0.5

Ring

<lang ring>

  1. Project : Integer roots
  2. Date  : 2017/10/09
  3. Author  : Gal Zsolt (~ CalmoSoft ~)
  4. Email  : <calmosoft@gmail.com>

see root(3, 8) see root(3, 9) see root(4, 167)

func root(n, x)

    for nr = floor(sqrt(x)) to 1 step -1
        if pow(nr, n) <= x
           see nr + nl
           exit
        ok
    next

</lang> Output:

2
2
3

Ruby

Translation of: Python, zkl

<lang ruby>def root(a,b)

 return b if b<2
 a1, c = a-1, 1
 f = -> x {(a1*x+b/(x**a1))/a}  # a lambda with argument x
 d = f[c]
 e = f[d]
 c, d, e = d, e, f[e] until [d,e].include?(c)
 [d,e].min

end

puts "First 2,001 digits of the square root of two:" puts root(2, 2*100**2000) </lang>

Output:
First 2,001 digits of the square root of two:
14142135623730950488016887242096(...)46758516447107578486024636008

Scheme

Translation of: Python

<lang scheme>(define (root a b)

 (define // quotient)
 (define (y a a1 b c d e)
   (if (or (= c d) (= c e))
     (min d e)
     (y a a1 b d e (// (+ (* a1 e) (// b (expt e a1))) a))))
 (if (< b 2)
   b
   (let* ((a1 (- a 1))
          (c 1)
          (d (// (+ (* a1 c) (// b (expt c a1))) a))
          (e (// (+ (* a1 d) (// b (expt d a1))) a)))
     (y a a1 b c d e))))
     

(display "First 2,001 digits of the cube root of two:\n") (display (root 3 (* 2 (expt 1000 2000))))</lang>

Output:
First 2,001 digits of the cube root of two:
125992104989487316476721060727822835057025146470150798008197511215529967651395948372939656243625509415431025603561566525939902404061373722845911030426935524696064261662500097747452656548030686718540551868924587251676419937370969509838278316139915512931369536618394746344857657030311909589598474110598116290705359081647801147352132548477129788024220858205325797252666220266900566560819947156281764050606648267735726704194862076214429656942050793191724414809204482328401274703219642820812019057141889964599983175038018886895942020559220211547299738488026073636974178877921579846750995396300782609596242034832386601398573634339097371265279959919699683779131681681544288502796515292781076797140020406056748039385612517183570069079849963419762914740448345402697154762285131780206438780476493225790528984670858052862581300054293885607206097472230406313572349364584065759169169167270601244028967000010690810353138529027004150842323362398893864967821941498380270729571768128790014457462271477023483571519055067220848184850092872392092826466067171742477537097370300127429180940544256965920750363575703751896037074739934610144901451576359604711119738452991329657262589048609788561801386773836157730098659836608059757560127871214868562426845564116515581793532280158962912994450040120842541416015752584162988142309735821530604057724253836453253356595511725228557956227724036656284687590154306675351908548451181817520429124123378096317252135754114181146612736604578303605744026513096070968164006888185657231009008428452608641405950336900307918699355691335183428569382625543135589735445023330285314932245513412195545782119650083395771426685063328419619686512109255789558850899686190154670043896878665545309854505763765036008943306510356935777537249548436821370317162162183495809356208726009626785183418345652239744540004476021778894208183802786665306532663261864116007400747475473558527701689502063754132232329694243701742343491617690600723853902227681129777413872079823430391031628546452083111122546828353183047061

Sidef

Translation of: Ruby

<lang ruby>func root(a, b) {

   b < 2 && return(b)
   var (a1, c) = (a-1, 1)
   var f = {|x| (a1*x + b//(x**a1)) // a }
   var d = f(c)
   var e = f(d)
   while (c !~ [d, e]) {
       (c, d, e) = (d, e, f(e))
   }
   [d, e].min

}

say "First 2,001 digits of the square root of two:" say root(2, 2 * 100**2000)</lang>

Output:
First 2,001 digits of the square root of two:
14142135623730950488016887242096980[...]32952546758516447107578486024636008

zkl

Translation of: Python

Uses GNU GMP library <lang zkl>var [const] BN=Import("zklBigNum"); fcn root(n,r){

  f:='wrap(z){ (n/z.pow(r-1) + z*(r-1))/r or 1 };  //--> v or 1
  c,d,e:=1,f(c),f(d);
  while(c!=d and c!=e){ c,d,e=d,e,f(e) }
  if(d<e) d else e

}</lang> <lang zkl>a:=BN(100).pow(2000)*2; println("Does GMP agree: ",root(a,3)==a.root(3));</lang>

Output:
Does GMP agree: True