Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Maintain the same structure and functionality when rewriting this code in C. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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... |
Convert this PowerShell block to C#, preserving its control flow and logic. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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] )... |
Preserve the algorithm and functionality while converting the code from PowerShell to C++. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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);
... |
Convert this PowerShell snippet to Java and keep its semantics consistent. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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;
... |
Convert this PowerShell block to Python, preserving its control flow and logic. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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
|
Rewrite the snippet below in VB so it works the same as the original PowerShell code. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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;... |
Maintain the same structure and functionality when rewriting this code in Go. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 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... |
Produce a language-to-language conversion: from R to C, same semantics. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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... |
Generate a C# translation of this R snippet without changing its computational steps. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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] )... |
Preserve the algorithm and functionality while converting the code from R to C++. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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);
... |
Produce a functionally identical Java code for the snippet given in R. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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;
... |
Rewrite the snippet below in Python so it works the same as the original R code. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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
|
Convert this R block to VB, preserving its control flow and logic. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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;... |
Generate an equivalent Go version of this R code. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.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... |
Port the provided Racket code into C while preserving the original functionality. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | #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... |
Change the following Racket code into C# without altering its purpose. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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] )... |
Convert the following code from Racket to C++, ensuring the logic remains intact. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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);
... |
Generate a Java translation of this Racket snippet without changing its computational steps. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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;
... |
Port the provided Racket code into Python while preserving the original functionality. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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 provided Racket code into VB while preserving the original functionality. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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;... |
Produce a language-to-language conversion: from Racket to Go, same semantics. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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... |
Port the provided COBOL code into C while preserving the original functionality. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | #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... |
Preserve the algorithm and functionality while converting the code from COBOL to C#. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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] )... |
Please provide an equivalent version of this COBOL code in C++. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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);
... |
Rewrite this program in Java while keeping its functionality equivalent to the COBOL version. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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;
... |
Can you help me rewrite this code in Python instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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 COBOL snippet without changing its computational steps. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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;... |
Rewrite the snippet below in Go so it works the same as the original COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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... |
Change the programming language of this snippet from REXX to C without modifying what it does. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | #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... |
Write the same algorithm in C# as shown in this REXX implementation. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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] )... |
Port the provided REXX code into C++ while preserving the original functionality. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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);
... |
Please provide an equivalent version of this REXX code in Java. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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;
... |
Keep all operations the same but rewrite the snippet in Python. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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 provided REXX code into VB while preserving the original functionality. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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;... |
Port the following code from REXX to Go with equivalent syntax and logic. |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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... |
Change the programming language of this snippet from Ruby to C without modifying what it does. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| #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... |
Rewrite the snippet below in C# so it works the same as the original Ruby code. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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] )... |
Change the programming language of this snippet from Ruby to C++ without modifying what it does. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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);
... |
Change the following Ruby code into Java without altering its purpose. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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;
... |
Convert this Ruby snippet to Python and keep its semantics consistent. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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 Ruby implementation. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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;... |
Rewrite the snippet below in Go so it works the same as the original Ruby code. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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... |
Generate a C translation of this Scala snippet without changing its computational steps. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | #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... |
Write the same code in C# as shown below in Scala. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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] )... |
Keep all operations the same but rewrite the snippet in C++. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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);
... |
Change the programming language of this snippet from Scala to Java without modifying what it does. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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;
... |
Change the programming language of this snippet from Scala to Python without modifying what it does. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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;... |
Ensure the translated Go code behaves exactly like the original Scala snippet. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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... |
Produce a functionally identical C code for the snippet given in Swift. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | #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... |
Convert this Swift block to C#, preserving its control flow and logic. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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] )... |
Produce a functionally identical C++ code for the snippet given in Swift. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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);
... |
Transform the following Swift implementation into Java, maintaining the same output and logic. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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;
... |
Convert this Swift block to Python, preserving its control flow and logic. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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 Swift to VB, same semantics. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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;... |
Change the programming language of this snippet from Swift to Go without modifying what it does. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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... |
Write the same algorithm in C as shown in this Tcl implementation. | proc nthroot {n A} {
expr {pow($A, 1.0/$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... |
Port the provided Tcl code into C# while preserving the original functionality. | proc nthroot {n A} {
expr {pow($A, 1.0/$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] )... |
Generate a C++ translation of this Tcl snippet without changing its computational steps. | proc nthroot {n A} {
expr {pow($A, 1.0/$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);
... |
Convert this Tcl block to Java, preserving its control flow and logic. | proc nthroot {n A} {
expr {pow($A, 1.0/$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;
... |
Convert this Tcl snippet to Python and keep its semantics consistent. | proc nthroot {n A} {
expr {pow($A, 1.0/$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
|
Can you help me rewrite this code in VB instead of Tcl, keeping it the same logically? | proc nthroot {n A} {
expr {pow($A, 1.0/$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;... |
Convert this Tcl block to Go, preserving its control flow and logic. | proc nthroot {n A} {
expr {pow($A, 1.0/$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... |
Preserve the algorithm and functionality while converting the code from Rust to PHP. |
fn nthRoot(n: f64, A: f64) -> f64 {
let p = 1e-9_f64 ;
let mut x0 = A / n ;
loop {
let mut x1 = ( (n-1.0) * x0 + A / f64::powf(x0, n-1.0) ) / n;
if (x1-x0).abs() < (x0*p).abs() { return x1 };
x0 = x1
}
}
fn main() {
println!("{}", nthRoot(3. , 8. ));
}
| 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];
}
|
Produce a functionally identical PHP code for the snippet given in 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);
... | 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 the same code in PHP as shown below in Arturo. | nthRoot: function [a,n][
N: to :floating n
result: a
x: a / N
while [0.000000000000001 < abs result-x][
x: result
result: (1//n) * add (n-1)*x a/pow x n-1
]
return result
]
print nthRoot 34.0 5
print nthRoot 42.0 10
print nthRoot 5.0 2
| 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];
}
|
Produce a language-to-language conversion: from AutoHotKey to PHP, same semantics. | p := 0.000001
MsgBox, % nthRoot( 10, 7131.5**10, p) "`n"
. nthRoot( 5, 34.0 , p) "`n"
. nthRoot( 2, 2 , p) "`n"
. nthRoot(0.5, 7 , p) "`n"
nthRoot(n, A, p) {
x1 := A
x2 := A / n
While Abs(x1 - x2) > p {
x1 := x2
x2 := ((n-1)*x2+A/x2**(n-1)... | 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];
}
|
Port the provided AWK code into PHP while preserving the original functionality. |
BEGIN {
print nthroot(8,3)
print nthroot(16,2)
print nthroot(16,4)
print nthroot(125,3)
print nthroot(3,3)
print nthroot(3,2)
}
function nthroot(y,n) {
eps = 1e-15;
x = 1;
do {
d = ( y / ( x^(n-1) ) - x ) / n ;
x += d;
e = eps*x;
} while ( d < -e || d > e )
return ... | 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];
}
|
Maintain the same structure and functionality when rewriting this code in PHP. | *FLOAT 64
@% = &D0D
PRINT "Cube root of 5 is "; FNroot(3, 5, 0)
PRINT "125th root of 5643 is "; FNroot(125, 5643, 0)
END
DEF FNroot(n%, a, d)
LOCAL x0, x1 : x0 = a / n% :
REPEAT
x1 = ((n% - 1)*x0 + a/x0^(n%-1)) / n%
SWAP x0, x1
UNTIL ABS (x0 ... | 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];
}
|
Maintain the same structure and functionality when rewriting this code in PHP. | (ns test-project-intellij.core
(:gen-class))
(defn abs [x]
" Absolute value"
(if (< x 0) (- x) x))
(defn power [x n]
" x to power n, where n = 0, 1, 2, ... "
(apply * (repeat n x)))
(defn calc-delta [A x n]
" nth rooth algorithm delta calculation "
(/ (- (/ A (power x (- n 1))) x) n))
(defn nth-root
... | 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];
}
|
Rewrite the snippet below in PHP so it works the same as the original Common_Lisp code. | (defun nth-root (n a &optional (epsilon .0001) (guess (1- n)))
(assert (and (> n 1) (> a 0)))
(flet ((next (x)
(/ (+ (* (1- n) x)
(/ a (expt x (1- n))))
n)))
(do* ((xi guess xi+1)
(xi+1 (next xi) (next xi)))
((< (abs (- xi+1 xi)) epsilon) xi+1))))
| 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];
}
|
Translate the given D code snippet into PHP without altering its behavior. | import std.stdio, std.math;
real nthroot(in int n, in real A, in real p=0.001) pure nothrow {
real[2] x = [A, A / n];
while (abs(x[1] - x[0]) > p)
x = [x[1], ((n - 1) * x[1] + A / (x[1] ^^ (n-1))) / n];
return x[1];
}
void main() {
writeln(nthroot(10, 7131.5 ^^ 10));
writeln(nthroot(6, 64)... | 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];
}
|
Keep all operations the same but rewrite the snippet in PHP. | USES
Math;
function NthRoot(A, Precision: Double; n: Integer): Double;
var
x_p, X: Double;
begin
x_p := Sqrt(A);
while Abs(A - Power(x_p, n)) > Precision do
begin
x := (1/n) * (((n-1) * x_p) + (A/(Power(x_p, n - 1))));
x_p := x;
end;
Result := x_p;
end;
| 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 the same algorithm in PHP as shown in this Elixir implementation. | defmodule RC do
def nth_root(n, x, precision \\ 1.0e-5) do
f = fn(prev) -> ((n - 1) * prev + x / :math.pow(prev, (n-1))) / n end
fixed_point(f, x, precision, f.(x))
end
defp fixed_point(_, guess, tolerance, next) when abs(guess - next) < tolerance, do: next
defp fixed_point(f, _, tolerance, next), do... | 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];
}
|
Produce a functionally identical PHP code for the snippet given in Erlang. | fixed_point(F, Guess, Tolerance) ->
fixed_point(F, Guess, Tolerance, F(Guess)).
fixed_point(_, Guess, Tolerance, Next) when abs(Guess - Next) < Tolerance ->
Next;
fixed_point(F, _, Tolerance, Next) ->
fixed_point(F, Next, Tolerance, F(Next)).
| 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];
}
|
Produce a functionally identical PHP code for the snippet given in F#. | 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"
exi... | 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];
}
|
Transform the following Factor implementation into PHP, maintaining the same output and logic. | 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 ^ .
| 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];
}
|
Produce a language-to-language conversion: from Forth to PHP, 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.
| 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 the same algorithm in PHP as shown in this Fortran implementation. | 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
s... | 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];
}
|
Translate this program into PHP but keep the logic exactly as in Groovy. | 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 ... | 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 Haskell function in PHP with identical behavior. | n `nthRoot` x = fst $ until (uncurry(==)) (\(_,x0) -> (x0,((n-1)*x0+x/x0**(n-1))/n)) (x,x/n)
| 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];
}
|
Transform the following Icon implementation into PHP, maintaining the same output and logic. | 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,... | 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 the same algorithm in PHP 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
| 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];
}
|
Change the following Julia code into PHP without altering its purpose. | 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 =... | 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];
}
|
Keep all operations the same but rewrite the snippet in PHP. | function nroot(root, num)
return num^(1/root)
end
| 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];
}
|
Keep all operations the same but rewrite the snippet in PHP. | Root[A,n]
| 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];
}
|
Port the following code from MATLAB to PHP 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
| 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];
}
|
Preserve the algorithm and functionality while converting the code from Nim to PHP. | 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)
| 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];
}
|
Translate this program into PHP but keep the logic exactly as 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" ... | 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];
}
|
Please provide an equivalent version of this Perl code in PHP. | 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;
}
}
| 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];
}
|
Change the following PowerShell code into PHP without altering its purpose. |
$epsilon=1E-10
function power($x,$e){
$ret=1
for($i=1;$i -le $e;$i++){
$ret*=$x
}
return $ret
}
function root($y,$n){
if (0+$n -lt 0){$tmp=-$n} else {$tmp=$n}
$ans=1
do{
$d = ($y/(power $ans ($tmp-1)) - $ans)/$tmp
$ans+=$d
} while ($d -lt -$epsilon -or $d -gt $epsilon)
if (0+$n -lt 0... | 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];
}
|
Convert this R block to PHP, preserving its control flow and logic. | nthroot <- function(A, n, tol=sqrt(.Machine$double.eps))
{
ifelse(A < 1, x0 <- A * n, x0 <- A / n)
repeat
{
x1 <- ((n-1)*x0 + A / x0^(n-1))/n
if(abs(x1 - x0) > tol) x0 <- x1 else break
}
x1
}
nthroot(7131.5^10, 10)
nthroot(7, 0.5)
| 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];
}
|
Preserve the algorithm and functionality while converting the code from Racket to PHP. | #lang racket
(define (nth-root number root (tolerance 0.001))
(define (acceptable? next current)
(< (abs (- next current)) tolerance))
(define (improve current)
(/ (+ (* (- root 1) current) (/ number (expt current (- root 1)))) root))
(define (loop current)
(define next-guess (improve current))... | 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];
}
|
Convert this COBOL snippet to PHP and keep its semantics consistent. | IDENTIFICATION DIVISION.
PROGRAM-ID. Nth-Root.
AUTHOR. Bill Gunshannon.
INSTALLATION.
DATE-WRITTEN. 4 Feb 2020.
************************************************************
** Program Abstract:
** Compute the Nth Root of a positive real number.
**
... | 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];
}
|
Can you help me rewrite this code in PHP instead of REXX, keeping it the same logically? |
class nth_root
method main(args=String[]) static
if args.length < 2 then
do
say "at least 2 arguments expected"
exit
end
x = args[0]
root = args[1]
if args.length > 2 then digs = args[2]
if root=='' then root=2
if digs = null, digs = '' then digs=20
numeric digits digs
... | 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];
}
|
Rewrite this program in PHP while keeping its functionality equivalent to the Ruby version. | def nthroot(n, a, precision = 1e-5)
x = Float(a)
begin
prev = x
x = ((n - 1) * prev + a / (prev ** (n - 1))) / n
end while (prev - x).abs > precision
x
end
p nthroot(5,34)
| 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];
}
|
Keep all operations the same but rewrite the snippet in PHP. |
fun nthRoot(x: Double, n: Int): Double {
if (n < 2) throw IllegalArgumentException("n must be more than 1")
if (x <= 0.0) throw IllegalArgumentException("x must be positive")
val np = n - 1
fun iter(g: Double) = (np * g + x / Math.pow(g, np.toDouble())) / n
var g1 = x
var g2 = iter(g1)
whi... | 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];
}
|
Convert the following code from Swift to PHP, ensuring the logic remains intact. | extension FloatingPoint where Self: ExpressibleByFloatLiteral {
@inlinable
public func power(_ e: Int) -> Self {
var res = Self(1)
for _ in 0..<e {
res *= self
}
return res
}
@inlinable
public func root(n: Int, epsilon: Self = 2.220446049250313e-16) -> Self {
var d = Self(0)
v... | 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];
}
|
Maintain the same structure and functionality when rewriting this code in PHP. | proc nthroot {n A} {
expr {pow($A, 1.0/$n)}
}
| 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];
}
|
Convert this C block to Rust, preserving its control flow and logic. | #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... |
fn nthRoot(n: f64, A: f64) -> f64 {
let p = 1e-9_f64 ;
let mut x0 = A / n ;
loop {
let mut x1 = ( (n-1.0) * x0 + A / f64::powf(x0, n-1.0) ) / n;
if (x1-x0).abs() < (x0*p).abs() { return x1 };
x0 = x1
}
}
fn main() {
println!("{}", nthRoot(3. , 8. ));
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.