WeixiangYan/CodeTransOcean huggingface dataset:
Task: Fivenum








VB: #define floor(x) ((x*2.0-0.5) Shr 1)

Sub rapidSort (array()As Single, l As Integer, r As Integer)
    Dim As Integer n, wert, nptr, rep
    Dim As Single arr, LoVal = array(l), HiVal = array(r)
    For n = l To r
        If LoVal > array(n) Then LoVal = array(n)
        If HiVal < array(n) Then HiVal = array(n)
    Next n
    Redim SortArray(LoVal To HiVal) As Single
    For n = l To r
        wert = array(n)
        SortArray(wert) += 1
    Next n
    nptr = l-1
    For arr = LoVal To HiVal
        rep = SortArray(arr)
        For n = 1 To rep
            nptr += 1
            array(nptr) = arr
        Next n
    Next arr
    Erase SortArray
End Sub

Function median(tbl() As Single, lo As Integer, hi As Integer) As Single
    Dim As Integer l = hi-lo+1
    Dim As Integer m = lo+floor(l/2)
    
    If l Mod 2 = 1 Then Return tbl(m)
    Return (tbl(m-1)+tbl(m))/2
End Function

Sub fivenum(tbl() As Single)
    rapidSort(tbl(), Lbound(tbl), Ubound(tbl))
    Dim As Integer l = Ubound(tbl)
    Dim As Single m = floor(l/2) + (l Mod 2)
    Dim As Single r1,r2,r3,r4,r5
    
    r1 = tbl(1)
    r2 = median(tbl(),1,m)
    r3 = median(tbl(),1,l)
    r4 = median(tbl(),m+1,l)
    r5 = tbl(l)
    
    Print "[" & r1; ","; r2; ","; r3; ","; r4; ", "; r5 & "]"
End Sub

Dim As Single x1(1 To ...) = {15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
Dim As Single x2(1 To ...) = {36, 40, 7, 39, 41, 15}
Dim As Single x3(1 To ...) = {_
0.14082834,  0.09748790,  1.73131507,  0.87636009, -1.95059594, _
0.73438555, -0.03035726,  1.46675970, -0.74621349, -0.72588772, _
0.63905160,  0.61501527, -0.98983780, -1.00447874, -0.62759469, _
0.66206163,  1.04312009, -0.10305385,  0.75775634,  0.32566578}
fivenum(x1())
fivenum(x2())
fivenum(x3())

Sleep
Task: Fivenum




Go: package main

import (
    "fmt"
    "math"
    "sort"
)

func fivenum(a []float64) (n5 [5]float64) {
    sort.Float64s(a)
    n := float64(len(a))
    n4 := float64((len(a)+3)/2) / 2
    d := []float64{1, n4, (n + 1) / 2, n + 1 - n4, n}
    for e, de := range d {
        floor := int(de - 1)
        ceil := int(math.Ceil(de - 1))
        n5[e] = .5 * (a[floor] + a[ceil])
    }
    return
}

var (
    x1 = []float64{36, 40, 7, 39, 41, 15}
    x2 = []float64{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}
    x3 = []float64{
        0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594,
        0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772,
        0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469,
        0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578,
    }
)

func main() {
    fmt.Println(fivenum(x1))
    fmt.Println(fivenum(x2))
    fmt.Println(fivenum(x3))
}
Task: Fivenum








VB: Option Base 1
Private Function median(tbl As Variant, lo As Integer, hi As Integer)
    Dim l As Integer: l = hi - lo + 1
    Dim m As Integer: m = lo + WorksheetFunction.Floor_Precise(l / 2)
    If l Mod 2 = 1 Then
        median = tbl(m)
    Else
        median = (tbl(m - 1) + tbl(m)) / 2
    End if 
End Function
Private Function fivenum(tbl As Variant) As Variant
    Sort tbl, UBound(tbl)
    Dim l As Integer: l = UBound(tbl)
    Dim m As Integer: m = WorksheetFunction.Floor_Precise(l / 2) + l Mod 2
    Dim r(5) As String
    r(1) = CStr(tbl(1))
    r(2) = CStr(median(tbl, 1, m))
    r(3) = CStr(median(tbl, 1, l))
    r(4) = CStr(median(tbl, m + 1, l))
    r(5) = CStr(tbl(l))
    fivenum = r
End Function
Public Sub main()
    Dim x1 As Variant, x2 As Variant, x3 As Variant
    x1 = [{15, 6, 42, 41, 7, 36, 49, 40, 39, 47, 43}]
    x2 = [{36, 40, 7, 39, 41, 15}]
    x3 = [{0.14082834, 0.09748790, 1.73131507, 0.87636009, -1.95059594, 0.73438555, -0.03035726, 1.46675970, -0.74621349, -0.72588772, 0.63905160, 0.61501527, -0.98983780, -1.00447874, -0.62759469, 0.66206163, 1.04312009, -0.10305385, 0.75775634, 0.32566578}]
    Debug.Print Join(fivenum(x1), " | ")
    Debug.Print Join(fivenum(x2), " | ")
    Debug.Print Join(fivenum(x3), " | ")
End Sub
Task: Nth root



C#: static void Main(string[] args)
{
	Console.WriteLine(NthRoot(81,2,.001));
        Console.WriteLine(NthRoot(1000,3,.001));
        Console.ReadLine();
}

public static double NthRoot(double A,int n,  double p)
{
	double _n= (double) n;
	double[] x = new double[2];		
	x[0] = A;
	x[1] = A/_n;
	while(Math.Abs(x[0] -x[1] ) > p)
	{
		x[1] = x[0];
		x[0] = (1/_n)*(((_n-1)*x[1]) + (A/Math.Pow(x[1],_n-1)));
			
	}
	return x[0];
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root

C: #include <stdio.h>
#include <float.h>

double pow_ (double x, int e) {
    int i;
    double r = 1;
    for (i = 0; i < e; i++) {
        r *= x;
    }
    return r;
}

double root (int n, double x) {
    double d, r = 1;
    if (!x) {
        return 0;
    }
    if (n < 1 || (x < 0 && !(n&1))) {
        return 0.0 / 0.0; 
    }
    do {
        d = (x / pow_(r, n - 1) - r) / n;
        r += d;
    }
    while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
    return r;
}

int main () {
    int n = 15;
    double x = pow_(-3.14159, 15);
    printf("root(%d, %g) = %g\n", n, x, root(n, x));
    return 0;
}











Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root


C++: double NthRoot(double m_nValue, double index, double guess, double pc)
   {
       double result = guess;
       double result_next;
       do
       {
           result_next = (1.0/index)*((index-1.0)*result+(m_nValue)/(pow(result,(index-1.0))));
           result = result_next;
           pc--;
       }while(pc>1);
       return result;
   };










Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root




Go: func root(a float64, n int) float64 {
    n1 := n - 1
    n1f, rn := float64(n1), 1/float64(n)
    x, x0 := 1., 0.
    for {
        potx, t2 := 1/x, a
        for b := n1; b > 0; b >>= 1 {
            if b&1 == 1 {
                t2 *= potx
            }
            potx *= potx
        }
        x0, x = x, rn*(n1f*x+t2)
        if math.Abs(x-x0)*1e15 < x {
            break
        }
    }
    return x
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root





Java: public static double nthroot(int n, double A) {
	return nthroot(n, A, .001);
}
public static double nthroot(int n, double A, double p) {
	if(A < 0) {
		System.err.println("A < 0");
		return -1;
	} else if(A == 0) {
		return 0;
	}
	double x_prev = A;
	double x = A / n;  
	while(Math.abs(x - x_prev) > p) {
		x_prev = x;
		x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;
	}
	return x;
}







Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root







Python: from decimal import Decimal, getcontext

def nthroot (n, A, precision):
    getcontext().prec = precision
    
    n = Decimal(n)
    x_0 = A / n 
    x_1 = 1     
    while True:
        
        x_0, x_1 = x_1, (1 / n)*((n - 1)*x_0 + (A / (x_0 ** (n - 1))))
        if x_0 == x_1:
            return x_1





Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root








VB: Private Function nth_root(y As Double, n As Double)
    Dim eps As Double: eps = 0.00000000000001 
    Dim x As Variant: x = 1
    Do While True
        d = (y / x ^ (n - 1) - x) / n
        x = x + d
        e = eps * x 
        If d > -e And d < e Then
            Exit Do
        End If
    Loop
    Debug.Print y; n; x; y ^ (1 / n)
End Function
Public Sub main()
    nth_root 1024, 10
    nth_root 27, 3
    nth_root 2, 2
    nth_root 5642, 125
    nth_root 7, 0.5
    nth_root 4913, 3
    nth_root 8, 3
    nth_root 16, 2
    nth_root 16, 4
    nth_root 125, 3
    nth_root 1000000000, 3
    nth_root 1000000000, 9
End Sub




Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Nth_Root is
   generic
      type Real is digits <>;
   function Nth_Root (Value : Real; N : Positive) return Real;
   
   function Nth_Root (Value : Real; N : Positive) return Real is
      type Index is mod 2;
      X : array (Index) of Real := (Value, Value);
      K : Index := 0;
   begin
      loop
         X (K + 1) := ( (Real (N) - 1.0) * X (K) + Value / X (K) ** (N-1) ) / Real (N);
         exit when X (K + 1) >= X (K);
         K := K + 1;
      end loop;
      return X (K + 1);
   end Nth_Root;

   function Long_Nth_Root is new Nth_Root (Long_Float);
begin
   Put_Line ("1024.0 10th  =" & Long_Float'Image (Long_Nth_Root (1024.0, 10)));
   Put_Line ("  27.0 3rd   =" & Long_Float'Image (Long_Nth_Root (27.0, 3)));
   Put_Line ("   2.0 2nd   =" & Long_Float'Image (Long_Nth_Root (2.0, 2)));
   Put_Line ("5642.0 125th =" & Long_Float'Image (Long_Nth_Root (5642.0, 125)));
end Test_Nth_Root;
Task: Nth root

C: #include <stdio.h>
#include <float.h>

double pow_ (double x, int e) {
    int i;
    double r = 1;
    for (i = 0; i < e; i++) {
        r *= x;
    }
    return r;
}

double root (int n, double x) {
    double d, r = 1;
    if (!x) {
        return 0;
    }
    if (n < 1 || (x < 0 && !(n&1))) {
        return 0.0 / 0.0; 
    }
    do {
        d = (x / pow_(r, n - 1) - r) / n;
        r += d;
    }
    while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
    return r;
}

int main () {
    int n = 15;
    double x = pow_(-3.14159, 15);
    printf("root(%d, %g) = %g\n", n, x, root(n, x));
    return 0;
}
Task: Nth root



C#: static void Main(string[] args)
{
	Console.WriteLine(NthRoot(81,2,.001));
        Console.WriteLine(NthRoot(1000,3,.001));
        Console.ReadLine();
}

public static double NthRoot(double A,int n,  double p)
{
	double _n= (double) n;
	double[] x = new double[2];		
	x[0] = A;
	x[1] = A/_n;
	while(Math.Abs(x[0] -x[1] ) > p)
	{
		x[1] = x[0];
		x[0] = (1/_n)*(((_n-1)*x[1]) + (A/Math.Pow(x[1],_n-1)));
			
	}
	return x[0];
}
Task: Nth root


C++: double NthRoot(double m_nValue, double index, double guess, double pc)
   {
       double result = guess;
       double result_next;
       do
       {
           result_next = (1.0/index)*((index-1.0)*result+(m_nValue)/(pow(result,(index-1.0))));
           result = result_next;
           pc--;
       }while(pc>1);
       return result;
   };
Task: Nth root





Java: public static double nthroot(int n, double A) {
	return nthroot(n, A, .001);
}
public static double nthroot(int n, double A, double p) {
	if(A < 0) {
		System.err.println("A < 0");
		return -1;
	} else if(A == 0) {
		return 0;
	}
	double x_prev = A;
	double x = A / n;  
	while(Math.abs(x - x_prev) > p) {
		x_prev = x;
		x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;
	}
	return x;
}
Task: Nth root







Python: from decimal import Decimal, getcontext

def nthroot (n, A, precision):
    getcontext().prec = precision
    
    n = Decimal(n)
    x_0 = A / n 
    x_1 = 1     
    while True:
        
        x_0, x_1 = x_1, (1 / n)*((n - 1)*x_0 + (A / (x_0 ** (n - 1))))
        if x_0 == x_1:
            return x_1
Task: Nth root








VB: Private Function nth_root(y As Double, n As Double)
    Dim eps As Double: eps = 0.00000000000001 
    Dim x As Variant: x = 1
    Do While True
        d = (y / x ^ (n - 1) - x) / n
        x = x + d
        e = eps * x 
        If d > -e And d < e Then
            Exit Do
        End If
    Loop
    Debug.Print y; n; x; y ^ (1 / n)
End Function
Public Sub main()
    nth_root 1024, 10
    nth_root 27, 3
    nth_root 2, 2
    nth_root 5642, 125
    nth_root 7, 0.5
    nth_root 4913, 3
    nth_root 8, 3
    nth_root 16, 2
    nth_root 16, 4
    nth_root 125, 3
    nth_root 1000000000, 3
    nth_root 1000000000, 9
End Sub
Task: Nth root




Go: func root(a float64, n int) float64 {
    n1 := n - 1
    n1f, rn := float64(n1), 1/float64(n)
    x, x0 := 1., 0.
    for {
        potx, t2 := 1/x, a
        for b := n1; b > 0; b >>= 1 {
            if b&1 == 1 {
                t2 *= potx
            }
            potx *= potx
        }
        x0, x = x, rn*(n1f*x+t2)
        if math.Abs(x-x0)*1e15 < x {
            break
        }
    }
    return x
}
Task: Nth root



C#: static void Main(string[] args)
{
	Console.WriteLine(NthRoot(81,2,.001));
        Console.WriteLine(NthRoot(1000,3,.001));
        Console.ReadLine();
}

public static double NthRoot(double A,int n,  double p)
{
	double _n= (double) n;
	double[] x = new double[2];		
	x[0] = A;
	x[1] = A/_n;
	while(Math.Abs(x[0] -x[1] ) > p)
	{
		x[1] = x[0];
		x[0] = (1/_n)*(((_n-1)*x[1]) + (A/Math.Pow(x[1],_n-1)));
			
	}
	return x[0];
}








Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest
Task: Nth root


C++: double NthRoot(double m_nValue, double index, double guess, double pc)
   {
       double result = guess;
       double result_next;
       do
       {
           result_next = (1.0/index)*((index-1.0)*result+(m_nValue)/(pow(result,(index-1.0))));
           result = result_next;
           pc--;
       }while(pc>1);
       return result;
   };









Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest
Task: Nth root

C: #include <stdio.h>
#include <float.h>

double pow_ (double x, int e) {
    int i;
    double r = 1;
    for (i = 0; i < e; i++) {
        r *= x;
    }
    return r;
}

double root (int n, double x) {
    double d, r = 1;
    if (!x) {
        return 0;
    }
    if (n < 1 || (x < 0 && !(n&1))) {
        return 0.0 / 0.0; 
    }
    do {
        d = (x / pow_(r, n - 1) - r) / n;
        r += d;
    }
    while (d >= DBL_EPSILON * 10 || d <= -DBL_EPSILON * 10);
    return r;
}

int main () {
    int n = 15;
    double x = pow_(-3.14159, 15);
    printf("root(%d, %g) = %g\n", n, x, root(n, x));
    return 0;
}










Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest
Task: Nth root




Go: func root(a float64, n int) float64 {
    n1 := n - 1
    n1f, rn := float64(n1), 1/float64(n)
    x, x0 := 1., 0.
    for {
        potx, t2 := 1/x, a
        for b := n1; b > 0; b >>= 1 {
            if b&1 == 1 {
                t2 *= potx
            }
            potx *= potx
        }
        x0, x = x, rn*(n1f*x+t2)
        if math.Abs(x-x0)*1e15 < x {
            break
        }
    }
    return x
}







Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest
Task: Nth root





Java: public static double nthroot(int n, double A) {
	return nthroot(n, A, .001);
}
public static double nthroot(int n, double A, double p) {
	if(A < 0) {
		System.err.println("A < 0");
		return -1;
	} else if(A == 0) {
		return 0;
	}
	double x_prev = A;
	double x = A / n;  
	while(Math.abs(x - x_prev) > p) {
		x_prev = x;
		x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n;
	}
	return x;
}






Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest
Task: Nth root







Python: from decimal import Decimal, getcontext

def nthroot (n, A, precision):
    getcontext().prec = precision
    
    n = Decimal(n)
    x_0 = A / n 
    x_1 = 1     
    while True:
        
        x_0, x_1 = x_1, (1 / n)*((n - 1)*x_0 + (A / (x_0 ** (n - 1))))
        if x_0 == x_1:
            return x_1




Fortran: program NthRootTest
  implicit none

  print *, nthroot(10, 7131.5**10)
  print *, nthroot(5, 34.0)

contains

  function nthroot(n, A, p)
    real :: nthroot
    integer, intent(in)        :: n
    real, intent(in)           :: A
    real, intent(in), optional :: p

    real :: rp, x(2)

    if ( A < 0 ) then
       stop "A < 0"       
    elseif ( A == 0 ) then
       nthroot = 0
       return
    end if

    if ( present(p) ) then
       rp = p
    else
       rp = 0.001
    end if

    x(1) = A
    x(2) = A/n   

    do while ( abs(x(2) - x(1)) > rp )
       x(1) = x(2)
       x(2) = ((n-1.0)*x(2) + A/(x(2) ** (n-1.0)))/real(n)
    end do

    nthroot = x(2)

  end function nthroot

end program NthRootTest