Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Translate the given F# code snippet into VB without altering its behavior. | let nthroot n A =
let rec f x =
let m = n - 1.
let x' = (m * x + A/x**m) / n
match abs(x' - x) with
| t when t < abs(x * 1e-9) -> x'
| _ -> f x'
f (A / double n)
[<EntryPoint>]
let main args =
if args.Length <> 2 then
eprintfn "usage: nthroot n A"
exit 1
let (b, n) = System.Double.TryParse(args.[0])
let (b', A) = System.Double.TryParse(args.[1])
if (not b) || (not b') then
eprintfn "error: parameter must be a number"
exit 1
printf "%A" (nthroot n A)
0
| 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
|
Preserve the algorithm and functionality while converting the code from F# to Go. | let nthroot n A =
let rec f x =
let m = n - 1.
let x' = (m * x + A/x**m) / n
match abs(x' - x) with
| t when t < abs(x * 1e-9) -> x'
| _ -> f x'
f (A / double n)
[<EntryPoint>]
let main args =
if args.Length <> 2 then
eprintfn "usage: nthroot n A"
exit 1
let (b, n) = System.Double.TryParse(args.[0])
let (b', A) = System.Double.TryParse(args.[1])
if (not b) || (not b') then
eprintfn "error: parameter must be a number"
exit 1
printf "%A" (nthroot n A)
0
| 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
}
|
Keep all operations the same but rewrite the snippet in C. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| #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;
}
|
Translate the given Factor code snippet into C# without altering its behavior. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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];
}
|
Rewrite the snippet below in C++ so it works the same as the original Factor code. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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;
};
|
Change the following Factor code into Java without altering its purpose. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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;
}
|
Maintain the same structure and functionality when rewriting this code in Python. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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
|
Generate a VB translation of this Factor snippet without changing its computational steps. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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
|
Produce a functionally identical Go code for the snippet given in Factor. | USING: kernel locals math math.functions prettyprint ;
:: th-root ( a n -- a^1/n )
a [
a over n 1 - ^ /f
over n 1 - *
+ n /f
swap over 1e-5 ~ not
] loop ;
34 5 th-root .
34 5 recip ^ .
| 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
}
|
Produce a language-to-language conversion: from Forth to C, same semantics. | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| #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;
}
|
Convert the following code from Forth to C#, ensuring the logic remains intact. | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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];
}
|
Rewrite the snippet below in C++ so it works the same as the original Forth code. | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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;
};
|
Can you help me rewrite this code in Java instead of Forth, keeping it the same logically? | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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;
}
|
Maintain the same structure and functionality when rewriting this code in Python. | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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
|
Can you help me rewrite this code in VB instead of Forth, keeping it the same logically? | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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
|
Convert this Forth snippet to Go and keep its semantics consistent. | : th-root { F: a F: n -- a^1/n }
a
begin
a fover n 1e f- f** f/
fover n 1e f- f*
f+ n f/
fswap fover 1e-5 f~
until ;
34e 5e th-root f.
34e 5e 1/f f** f.
| 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
}
|
Generate a C# translation of this Fortran snippet without changing its computational steps. | 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
| 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];
}
|
Transform the following Fortran implementation into C++, maintaining the same output and logic. | 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
| 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;
};
|
Port the following code from Fortran to C with equivalent syntax and logic. | 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
| #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;
}
|
Maintain the same structure and functionality when rewriting this code in Go. | 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
| 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
}
|
Write the same code in Java as shown below in 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
| 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;
}
|
Convert this Fortran snippet to Python and keep its semantics consistent. | 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
| 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
|
Convert the following code from Fortran to PHP, ensuring the logic remains intact. | 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
| function nthroot($number, $root, $p = P)
{
$x[0] = $number;
$x[1] = $number/$root;
while(abs($x[1]-$x[0]) > $p)
{
$x[0] = $x[1];
$x[1] = (($root-1)*$x[1] + $number/pow($x[1], $root-1))/$root;
}
return $x[1];
}
|
Write a version of this Groovy function in C with identical behavior. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| #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;
}
|
Preserve the algorithm and functionality while converting the code from Groovy to C#. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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];
}
|
Port the following code from Groovy to C++ with equivalent syntax and logic. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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;
};
|
Ensure the translated Java code behaves exactly like the original Groovy snippet. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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;
}
|
Convert this Groovy block to Python, preserving its control flow and logic. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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
|
Write the same algorithm in VB as shown in this Groovy implementation. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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
|
Translate the given Groovy code snippet into Go without altering its behavior. | import static Constants.tolerance
import static java.math.RoundingMode.HALF_UP
def root(double base, double n) {
double xOld = 1
double xNew = 0
while (true) {
xNew = ((n - 1) * xOld + base/(xOld)**(n - 1))/n
if ((xNew - xOld).abs() < tolerance) { break }
xOld = xNew
}
(xNew as BigDecimal).setScale(7, HALF_UP)
}
| 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
}
|
Ensure the translated C code behaves exactly like the original Haskell snippet. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| #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;
}
|
Can you help me rewrite this code in C# instead of Haskell, keeping it the same logically? | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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];
}
|
Rewrite the snippet below in C++ so it works the same as the original Haskell code. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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;
};
|
Preserve the algorithm and functionality while converting the code from Haskell to Java. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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;
}
|
Write the same algorithm in Python as shown in this Haskell implementation. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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
|
Produce a language-to-language conversion: from Haskell to VB, same semantics. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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
|
Preserve the algorithm and functionality while converting the code from Haskell to Go. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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
}
|
Translate this program into C but keep the logic exactly as in Icon. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| #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;
}
|
Write a version of this Icon function in C# with identical behavior. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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];
}
|
Ensure the translated C++ code behaves exactly like the original Icon snippet. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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;
};
|
Preserve the algorithm and functionality while converting the code from Icon to Java. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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;
}
|
Generate a Python translation of this Icon snippet without changing its computational steps. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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
|
Ensure the translated VB code behaves exactly like the original Icon snippet. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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
|
Maintain the same structure and functionality when rewriting this code in Go. | procedure main()
showroot(125,3)
showroot(27,3)
showroot(1024,10)
showroot(39.0625,4)
showroot(7131.5^10,10)
end
procedure showroot(a,n)
printf("%i-th root of %i = %i\n",n,a,root(a,n))
end
procedure root(a,n,p)
if n < 0 | type(n) !== "integer" then runerr(101,n)
if a < 0 then runerr(205,a)
/p := 1e-14
xn := a / real(n)
while abs(a - xn^n) > p do
xn := ((n - 1) * (xi := xn) + a / (xi ^ (n-1))) / real(n)
return xn
end
link printf
| 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
}
|
Change the programming language of this snippet from J to C without modifying what it does. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| #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;
}
|
Write the same code in C# as shown below in J. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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];
}
|
Port the following code from J to C++ with equivalent syntax and logic. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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;
};
|
Write the same algorithm in Java as shown in this J implementation. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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;
}
|
Rewrite this program in Python while keeping its functionality equivalent to the J version. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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
|
Change the programming language of this snippet from J to VB without modifying what it does. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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
|
Port the provided J code into Go while preserving the original functionality. | '`N X NP' =. (0 { [)`(1 { [)`(2 { [)
iter =. N %~ (NP * ]) + X % ] ^ NP
nth_root =: (, , _1+[) iter^:_ f. ]
10 nth_root 7131.5^10
7131.5
| 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
}
|
Change the programming language of this snippet from Julia to C without modifying what it does. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| #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;
}
|
Translate this program into C# but keep the logic exactly as in Julia. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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];
}
|
Port the provided Julia code into C++ while preserving the original functionality. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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;
};
|
Rewrite the snippet below in Java so it works the same as the original Julia code. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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;
}
|
Generate an equivalent Python version of this Julia code. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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
|
Generate a VB translation of this Julia snippet without changing its computational steps. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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
|
Port the provided Julia code into Go while preserving the original functionality. | function nthroot(n::Integer, r::Real)
r < 0 || n == 0 && throw(DomainError())
n < 0 && return 1 / nthroot(-n, r)
r > 0 || return 0
x = r / n
prevdx = r
while true
y = x ^ (n - 1)
dx = (r - y * x) / (n * y)
abs(dx) ≥ abs(prevdx) && return x
x += dx
prevdx = dx
end
end
@show nthroot.(-5:2:5, 5.0)
@show nthroot.(-5:2:5, 5.0) - 5.0 .^ (1 ./ (-5:2:5))
| 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
}
|
Convert this Lua block to C, preserving its control flow and logic. | function nroot(root, num)
return num^(1/root)
end
| #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;
}
|
Produce a language-to-language conversion: from Lua to C#, same semantics. | function nroot(root, num)
return num^(1/root)
end
| 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];
}
|
Maintain the same structure and functionality when rewriting this code in C++. | function nroot(root, num)
return num^(1/root)
end
| 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;
};
|
Maintain the same structure and functionality when rewriting this code in Java. | function nroot(root, num)
return num^(1/root)
end
| 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;
}
|
Convert the following code from Lua to Python, ensuring the logic remains intact. | function nroot(root, num)
return num^(1/root)
end
| 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
|
Maintain the same structure and functionality when rewriting this code in VB. | function nroot(root, num)
return num^(1/root)
end
| 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
|
Port the following code from Lua to Go with equivalent syntax and logic. | function nroot(root, num)
return num^(1/root)
end
| 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
}
|
Convert this Mathematica snippet to C and keep its semantics consistent. | Root[A,n]
| #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;
}
|
Produce a functionally identical C# code for the snippet given in Mathematica. | Root[A,n]
| 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];
}
|
Port the following code from Mathematica to C++ with equivalent syntax and logic. | Root[A,n]
| 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;
};
|
Generate a Java translation of this Mathematica snippet without changing its computational steps. | Root[A,n]
| 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;
}
|
Convert this Mathematica snippet to Python and keep its semantics consistent. | Root[A,n]
| 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
|
Rewrite the snippet below in VB so it works the same as the original Mathematica code. | Root[A,n]
| 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
|
Change the programming language of this snippet from Mathematica to Go without modifying what it does. | Root[A,n]
| 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
}
|
Port the provided MATLAB code into C while preserving the original functionality. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| #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;
}
|
Can you help me rewrite this code in C# instead of MATLAB, keeping it the same logically? | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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];
}
|
Keep all operations the same but rewrite the snippet in C++. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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;
};
|
Preserve the algorithm and functionality while converting the code from MATLAB to Java. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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;
}
|
Port the following code from MATLAB to Python with equivalent syntax and logic. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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
|
Port the following code from MATLAB to VB with equivalent syntax and logic. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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
|
Convert this MATLAB block to Go, preserving its control flow and logic. | function answer = nthRoot(number,root)
format long
answer = number / root;
guess = number;
while not(guess == answer)
guess = answer;
answer = (1/root)*( ((root - 1)*guess) + ( number/(guess^(root - 1)) ) );
end
end
| 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
}
|
Maintain the same structure and functionality when rewriting this code in C. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| #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;
}
|
Preserve the algorithm and functionality while converting the code from Nim to C#. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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];
}
|
Write a version of this Nim function in C++ with identical behavior. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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;
};
|
Write a version of this Nim function in Java with identical behavior. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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;
}
|
Preserve the algorithm and functionality while converting the code from Nim to Python. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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
|
Please provide an equivalent version of this Nim code in VB. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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
|
Change the following Nim code into Go without altering its purpose. | import math
proc nthRoot(a: float; n: int): float =
var n = float(n)
result = a
var x = a / n
while abs(result-x) > 1e-15:
x = result
result = (1/n) * (((n-1)*x) + (a / pow(x, n-1)))
echo nthRoot(34.0, 5)
echo nthRoot(42.0, 10)
echo nthRoot(5.0, 2)
| 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
}
|
Write a version of this OCaml function in C with identical behavior. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| #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;
}
|
Change the following OCaml code into C# without altering its purpose. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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];
}
|
Rewrite this program in C++ while keeping its functionality equivalent to the OCaml version. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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;
};
|
Write the same code in Java as shown below in OCaml. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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;
}
|
Produce a functionally identical Python code for the snippet given in OCaml. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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
|
Ensure the translated VB code behaves exactly like the original OCaml snippet. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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
|
Change the programming language of this snippet from OCaml to Go without modifying what it does. | let nthroot ~n ~a ?(tol=0.001) () =
let nf = float n in let nf1 = nf -. 1.0 in
let rec iter x =
let x' = (nf1 *. x +. a /. (x ** nf1)) /. nf in
if tol > abs_float (x -. x') then x' else iter x' in
iter 1.0
;;
let () =
Printf.printf "%g\n" (nthroot 10 (7131.5 ** 10.0) ());
Printf.printf "%g\n" (nthroot 5 34.0 ());
;;
| 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
}
|
Rewrite the snippet below in C so it works the same as the original Perl code. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| #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;
}
|
Change the following Perl code into C# without altering its purpose. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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];
}
|
Generate an equivalent C++ version of this Perl code. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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;
};
|
Produce a functionally identical Java code for the snippet given in Perl. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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;
}
|
Generate a Python translation of this Perl snippet without changing its computational steps. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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
|
Port the following code from Perl to VB with equivalent syntax and logic. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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
|
Ensure the translated Go code behaves exactly like the original Perl snippet. | use strict;
sub nthroot ($$)
{
my ( $n, $A ) = @_;
my $x0 = $A / $n;
my $m = $n - 1.0;
while(1) {
my $x1 = ($m * $x0 + $A / ($x0 ** $m)) / $n;
return $x1 if abs($x1 - $x0) < abs($x0 * 1e-9);
$x0 = $x1;
}
}
| 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
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.