Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Write the same code in Python as shown below in OCaml.
let binomialCoeff n p = let p = if p < n -. p then p else n -. p in let rec cm res num denum = if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.) else res in cm 1. n 1.
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Produce a language-to-language conversion: from OCaml to VB, same semantics.
let binomialCoeff n p = let p = if p < n -. p then p else n -. p in let rec cm res num denum = if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.) else res in cm 1. n 1.
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Translate the given OCaml code snippet into Go without altering its behavior.
let binomialCoeff n p = let p = if p < n -. p then p else n -. p in let rec cm res num denum = if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.) else res in cm 1. n 1.
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Translate the given Perl code snippet into C without altering its behavior.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Convert the following code from Perl to C#, ensuring the logic remains intact.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Can you help me rewrite this code in C++ instead of Perl, keeping it the same logically?
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Convert this Perl block to Java, preserving its control flow and logic.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Change the programming language of this snippet from Perl to Python without modifying what it does.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Write the same algorithm in VB as shown in this Perl implementation.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Preserve the algorithm and functionality while converting the code from Perl to Go.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Produce a functionally identical C code for the snippet given in PowerShell.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Write the same code in C# as shown below in PowerShell.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Produce a functionally identical C++ code for the snippet given in PowerShell.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Produce a functionally identical Java code for the snippet given in PowerShell.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Produce a functionally identical Python code for the snippet given in PowerShell.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Preserve the algorithm and functionality while converting the code from PowerShell to VB.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Convert this PowerShell block to Go, preserving its control flow and logic.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Convert this R snippet to C and keep its semantics consistent.
choose(5,3)
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Convert this R block to C#, preserving its control flow and logic.
choose(5,3)
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Translate the given R code snippet into C++ without altering its behavior.
choose(5,3)
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Convert the following code from R to Java, ensuring the logic remains intact.
choose(5,3)
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Port the provided R code into Python while preserving the original functionality.
choose(5,3)
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Can you help me rewrite this code in VB instead of R, keeping it the same logically?
choose(5,3)
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Transform the following R implementation into Go, maintaining the same output and logic.
choose(5,3)
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Translate the given Racket code snippet into C without altering its behavior.
#lang racket (require math) (binomial 10 5)
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Generate an equivalent C# version of this Racket code.
#lang racket (require math) (binomial 10 5)
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Preserve the algorithm and functionality while converting the code from Racket to C++.
#lang racket (require math) (binomial 10 5)
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Produce a language-to-language conversion: from Racket to Java, same semantics.
#lang racket (require math) (binomial 10 5)
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Please provide an equivalent version of this Racket code in Python.
#lang racket (require math) (binomial 10 5)
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Write the same code in VB as shown below in Racket.
#lang racket (require math) (binomial 10 5)
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Rewrite the snippet below in Go so it works the same as the original Racket code.
#lang racket (require math) (binomial 10 5)
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Preserve the algorithm and functionality while converting the code from REXX to C.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Write a version of this REXX function in C# with identical behavior.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Produce a functionally identical C++ code for the snippet given in REXX.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Please provide an equivalent version of this REXX code in Java.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Please provide an equivalent version of this REXX code in Python.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Translate the given REXX code snippet into VB without altering its behavior.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Convert the following code from REXX to Go, ensuring the logic remains intact.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Keep all operations the same but rewrite the snippet in C.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Rewrite the snippet below in C# so it works the same as the original Ruby code.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Change the programming language of this snippet from Ruby to C++ without modifying what it does.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Translate this program into Java but keep the logic exactly as in Ruby.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Produce a functionally identical Python code for the snippet given in Ruby.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Rewrite the snippet below in VB so it works the same as the original Ruby code.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Preserve the algorithm and functionality while converting the code from Ruby to Go.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Port the provided Scala code into C while preserving the original functionality.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Keep all operations the same but rewrite the snippet in C#.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Can you help me rewrite this code in C++ instead of Scala, keeping it the same logically?
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Port the following code from Scala to Java with equivalent syntax and logic.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Keep all operations the same but rewrite the snippet in Python.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Transform the following Scala implementation into VB, maintaining the same output and logic.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Produce a language-to-language conversion: from Scala to Go, same semantics.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Port the provided Swift code into C while preserving the original functionality.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Change the programming language of this snippet from Swift to C# without modifying what it does.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Preserve the algorithm and functionality while converting the code from Swift to C++.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Rewrite the snippet below in Java so it works the same as the original Swift code.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Maintain the same structure and functionality when rewriting this code in Python.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Translate the given Swift code snippet into VB without altering its behavior.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Change the following Swift code into Go without altering its purpose.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Write a version of this Tcl function in C with identical behavior.
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
#include <stdio.h> #include <limits.h> static unsigned long gcd_ui(unsigned long x, unsigned long y) { unsigned long t; if (y < x) { t = x; x = y; y = t; } while (y > 0) { t = y; y = x % y; x = t; } return x; } unsigned long binomial(unsigned long n, unsigned long k) { unsigned long d, g, r = 1; if (k == 0) return 1; if (k == 1) return n; if (k >= n) return (k == n); if (k > n/2) k = n-k; for (d = 1; d <= k; d++) { if (r >= ULONG_MAX/n) { unsigned long nr, dr; g = gcd_ui(n, d); nr = n/g; dr = d/g; g = gcd_ui(r, dr); r = r/g; dr = dr/g; if (r >= ULONG_MAX/nr) return 0; r *= nr; r /= dr; n--; } else { r *= n--; r /= d; } } return r; } int main() { printf("%lu\n", binomial(5, 3)); printf("%lu\n", binomial(40, 19)); printf("%lu\n", binomial(67, 31)); return 0; }
Rewrite the snippet below in C# so it works the same as the original Tcl code.
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
using System; namespace BinomialCoefficients { class Program { static void Main(string[] args) { ulong n = 1000000, k = 3; ulong result = biCoefficient(n, k); Console.WriteLine("The Binomial Coefficient of {0}, and {1}, is equal to: {2}", n, k, result); Console.ReadLine(); } static int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); } static ulong biCoefficient(ulong n, ulong k) { if (k > n - k) { k = n - k; } ulong c = 1; for (uint i = 0; i < k; i++) { c = c * (n - i); c = c / (i + 1); } return c; } } }
Can you help me rewrite this code in C++ instead of Tcl, keeping it the same logically?
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
double Factorial(double nValue) { double result = nValue; double result_next; double pc = nValue; do { result_next = result*(pc-1); result = result_next; pc--; }while(pc>2); nValue = result; return nValue; } double binomialCoefficient(double n, double k) { if (abs(n - k) < 1e-7 || k < 1e-7) return 1.0; if( abs(k-1.0) < 1e-7 || abs(k - (n-1)) < 1e-7)return n; return Factorial(n) /(Factorial(k)*Factorial((n - k))); }
Port the following code from Tcl to Java with equivalent syntax and logic.
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
public class Binomial { private static long binomialInt(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static Object binomialIntReliable(int n, int k) { if (k > n - k) k = n - k; long binom = 1; for (int i = 1; i <= k; i++) { try { binom = Math.multiplyExact(binom, n + 1 - i) / i; } catch (ArithmeticException e) { return "overflow"; } } return binom; } private static double binomialFloat(int n, int k) { if (k > n - k) k = n - k; double binom = 1.0; for (int i = 1; i <= k; i++) binom = binom * (n + 1 - i) / i; return binom; } private static BigInteger binomialBigInt(int n, int k) { if (k > n - k) k = n - k; BigInteger binom = BigInteger.ONE; for (int i = 1; i <= k; i++) { binom = binom.multiply(BigInteger.valueOf(n + 1 - i)); binom = binom.divide(BigInteger.valueOf(i)); } return binom; } private static void demo(int n, int k) { List<Object> data = Arrays.asList( n, k, binomialInt(n, k), binomialIntReliable(n, k), binomialFloat(n, k), binomialBigInt(n, k)); System.out.println(data.stream().map(Object::toString).collect(Collectors.joining("\t"))); } public static void main(String[] args) { demo(5, 3); demo(1000, 300); } }
Translate the given Tcl code snippet into Python without altering its behavior.
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
def binomialCoeff(n, k): result = 1 for i in range(1, k+1): result = result * (n-i+1) / i return result if __name__ == "__main__": print(binomialCoeff(5, 3))
Can you help me rewrite this code in VB instead of Tcl, keeping it the same logically?
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
Function binomial(n,k) binomial = factorial(n)/(factorial(n-k)*factorial(k)) End Function Function factorial(n) If n = 0 Then factorial = 1 Else For i = n To 1 Step -1 If i = n Then factorial = n Else factorial = factorial * i End If Next End If End Function WScript.StdOut.Write "the binomial coefficient of 5 and 3 = " & binomial(5,3) WScript.StdOut.WriteLine
Translate this program into Go but keep the logic exactly as in Tcl.
package require Tcl 8.5 proc binom {n k} { set pTop 1 for {set i $n} {$i > $n - $k} {incr i -1} { set pTop [expr {$pTop * $i}] } set pBottom 1 for {set i $k} {$i > 1} {incr i -1} { set pBottom [expr {$pBottom * $i}] } return [expr {$pTop / $pBottom}] }
package main import "fmt" import "math/big" func main() { fmt.Println(new(big.Int).Binomial(5, 3)) fmt.Println(new(big.Int).Binomial(60, 30)) }
Write the same code in PHP as shown below in Rust.
fn fact(n:u32) -> u64 { let mut f:u64 = n as u64; for i in 2..n { f *= i as u64; } return f; } fn choose(n: u32, k: u32) -> u64 { let mut num:u64 = n as u64; for i in 1..k { num *= (n-i) as u64; } return num / fact(k); } fn main() { println!("{}", choose(5,3)); }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate the given Ada code snippet into PHP without altering its behavior.
with Ada.Text_IO; use Ada.Text_IO; procedure Test_Binomial is function Binomial (N, K : Natural) return Natural is Result : Natural := 1; M : Natural; begin if N < K then raise Constraint_Error; end if; if K > N/2 then M := N - K; else M := K; end if; for I in 1..M loop Result := Result * (N - M + I) / I; end loop; return Result; end Binomial; begin for N in 0..17 loop for K in 0..N loop Put (Integer'Image (Binomial (N, K))); end loop; New_Line; end loop; end Test_Binomial;
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Keep all operations the same but rewrite the snippet in PHP.
factorial: function [n]-> product 1..n binomial: function [x,y]-> (factorial x) / (factorial y) * factorial x-y print binomial 5 3
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Keep all operations the same but rewrite the snippet in PHP.
MsgBox, % Round(BinomialCoefficient(5, 3)) BinomialCoefficient(n, k) { r := 1 Loop, % k < n - k ? k : n - k { r *= n - A_Index + 1 r /= A_Index } Return, r }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the following AWK code into PHP without altering its purpose.
BEGIN { main(5,3) main(100,2) main(33,17) exit(0) } function main(n,k, i,r) { r = 1 for (i=1; i<k+1; i++) { r *= (n - i + 1) / i } printf("%d %d = %d\n",n,k,r) }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write the same algorithm in PHP as shown in this BBC_Basic implementation.
@%=&1010 PRINT "Binomial (5,3) = "; FNbinomial(5, 3) PRINT "Binomial (100,2) = "; FNbinomial(100, 2) PRINT "Binomial (33,17) = "; FNbinomial(33, 17) END DEF FNbinomial(N%, K%) LOCAL R%, D% R% = 1 : D% = N% - K% IF D% > K% THEN K% = D% : D% = N% - K% WHILE N% > K% R% *= N% N% -= 1 WHILE D% > 1 AND (R% MOD D%) = 0 R% /= D% D% -= 1 ENDWHILE ENDWHILE = R%
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Produce a language-to-language conversion: from Clojure to PHP, same semantics.
(defn binomial-coefficient [n k] (let [rprod (fn [a b] (reduce * (range a (inc b))))] (/ (rprod (- n k -1) n) (rprod 1 k))))
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write the same code in PHP as shown below in Common_Lisp.
(defun fac (n) (if (zp n) 1 (* n (fac (1- n))))) (defun binom (n k) (/ (fac n) (* (fac (- n k)) (fac k)))
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Produce a functionally identical PHP code for the snippet given in D.
T binomial(T)(in T n, T k) pure nothrow { if (k > (n / 2)) k = n - k; T bc = 1; foreach (T i; T(2) .. k + 1) bc = (bc * (n - k + i)) / i; return bc; } void main() { import std.stdio, std.bigint; foreach (const d; [[5, 3], [100, 2], [100, 98]]) writefln("(%3d %3d) = %s", d[0], d[1], binomial(d[0], d[1])); writeln("(100 50) = ", binomial(100.BigInt, 50.BigInt)); }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Maintain the same structure and functionality when rewriting this code in PHP.
program Binomial; function BinomialCoff(N, K: Cardinal): Cardinal; var L: Cardinal; begin if N < K then Result:= 0 else begin if K > N - K then K:= N - K; Result:= 1; L:= 0; while L < K do begin Result:= Result * (N - L); Inc(L); Result:= Result div L; end; end; end; begin Writeln('C(5,3) is ', BinomialCoff(5, 3)); ReadLn; end.
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate this program into PHP but keep the logic exactly as in Elixir.
defmodule RC do def choose(n,k) when is_integer(n) and is_integer(k) and n>=0 and k>=0 and n>=k do if k==0, do: 1, else: choose(n,k,1,1) end def choose(n,k,k,acc), do: div(acc * (n-k+1), k) def choose(n,k,i,acc), do: choose(n, k, i+1, div(acc * (n-i+1), i)) end IO.inspect RC.choose(5,3) IO.inspect RC.choose(60,30)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate the given Erlang code snippet into PHP without altering its behavior.
choose(N, 0) -> 1; choose(N, K) when is_integer(N), is_integer(K), (N >= 0), (K >= 0), (N >= K) -> choose(N, K, 1, 1). choose(N, K, K, Acc) -> (Acc * (N-K+1)) div K; choose(N, K, I, Acc) -> choose(N, K, I+1, (Acc * (N-I+1)) div I).
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the following F# code into PHP without altering its purpose.
let choose n k = List.fold (fun s i -> s * (n-i+1)/i ) 1 [1..k]
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the following Factor code into PHP without altering its purpose.
: fact ( n -- n-factorial ) dup 0 = [ drop 1 ] [ dup 1 - fact * ] if ; : choose ( n k -- n-choose-k ) 2dup - [ fact ] tri@ * / ; 5 3 choose . USE: math.ranges : choose-fold ( n k -- n-choose-k ) 2dup 1 + [a,b] product -rot - 1 [a,b] product / ;
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the programming language of this snippet from Forth to PHP without modifying what it does.
: choose 1 swap 0 ?do over i - i 1+ */ loop nip ; 5 3 choose . 33 17 choose .
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write the same algorithm in PHP as shown in this Fortran implementation.
program test_choose implicit none write (*, '(i0)') choose (5, 3) contains function factorial (n) result (res) implicit none integer, intent (in) :: n integer :: res integer :: i res = product ((/(i, i = 1, n)/)) end function factorial function choose (n, k) result (res) implicit none integer, intent (in) :: n integer, intent (in) :: k integer :: res res = factorial (n) / (factorial (k) * factorial (n - k)) end function choose end program test_choose
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Port the following code from Groovy to PHP with equivalent syntax and logic.
def factorial = { x -> assert x > -1 x == 0 ? 1 : (1..x).inject(1G) { BigInteger product, BigInteger factor -> product *= factor } } def combinations = { n, k -> assert k >= 0 assert n >= k factorial(n).intdiv(factorial(k)*factorial(n-k)) }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the following Haskell code into PHP without altering its purpose.
choose :: (Integral a) => a -> a -> a choose n k = product [k+1..n] `div` product [1..n-k]
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate this program into PHP but keep the logic exactly as in Icon.
link math, factors procedure main() write("choose(5,3)=",binocoef(5,3)) end
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Produce a language-to-language conversion: from J to PHP, same semantics.
3 ! 5 10
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Port the following code from Julia to PHP with equivalent syntax and logic.
@show binomial(5, 3)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Rewrite this program in PHP while keeping its functionality equivalent to the Lua version.
function Binomial( n, k ) if k > n then return nil end if k > n/2 then k = n - k end numer, denom = 1, 1 for i = 1, k do numer = numer * ( n - i + 1 ) denom = denom * i end return numer / denom end
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write a version of this Mathematica function in PHP with identical behavior.
(Local) In[1]:= Binomial[5,3] (Local) Out[1]= 10
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate the given MATLAB code snippet into PHP without altering its behavior.
>> nchoosek(5,3) ans = 10
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Preserve the algorithm and functionality while converting the code from Nim to PHP.
proc binomialCoeff(n, k: int): int = result = 1 for i in 1..k: result = result * (n-i+1) div i echo binomialCoeff(5, 3)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Produce a functionally identical PHP code for the snippet given in OCaml.
let binomialCoeff n p = let p = if p < n -. p then p else n -. p in let rec cm res num denum = if denum <= p then cm ((res *. num) /. denum) (num -. 1.) (denum +. 1.) else res in cm 1. n 1.
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Port the provided Perl code into PHP while preserving the original functionality.
sub binomial { use bigint; my ($r, $n, $k) = (1, @_); for (1 .. $k) { $r *= $n--; $r /= $_ } $r; } print binomial(5, 3);
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write the same algorithm in PHP as shown in this PowerShell implementation.
function choose($n,$k) { if($k -le $n -and 0 -le $k) { $numerator = $denominator = 1 0..($k-1) | foreach{ $numerator *= ($n-$_) $denominator *= ($_ + 1) } $numerator/$denominator } else { "$k is greater than $n or lower than 0" } } choose 5 3 choose 2 1 choose 10 10 choose 10 2 choose 10 8
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Transform the following R implementation into PHP, maintaining the same output and logic.
choose(5,3)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Change the programming language of this snippet from Racket to PHP without modifying what it does.
#lang racket (require math) (binomial 10 5)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate this program into PHP but keep the logic exactly as in REXX.
numeric digits 100000 parse arg n k . say 'combinations('n","k')=' comb(n,k) exit comb: procedure; parse arg x,y; return !(x) % (!(x-y) * !(y)) !: procedure; !=1; do j=2 to arg(1); !=!*j; end
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Translate this program into PHP but keep the logic exactly as in Ruby.
class Integer def choose(k) pTop = (self-k+1 .. self).inject(1, &:*) pBottom = (2 .. k).inject(1, &:*) pTop / pBottom end end p 5.choose(3) p 60.choose(30)
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Write a version of this Scala function in PHP with identical behavior.
fun binomial(n: Int, k: Int) = when { n < 0 || k < 0 -> throw IllegalArgumentException("negative numbers not allowed") n == k -> 1L else -> { val kReduced = min(k, n - k) var result = 1L var numerator = n var denominator = 1 while (denominator <= kReduced) result = result * numerator-- / denominator++ result } } fun main(args: Array<String>) { for (n in 0..14) { for (k in 0..n) print("%4d ".format(binomial(n, k))) println() } }
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>
Transform the following Swift implementation into PHP, maintaining the same output and logic.
func factorial<T: BinaryInteger>(_ n: T) -> T { guard n != 0 else { return 1 } return stride(from: n, to: 0, by: -1).reduce(1, *) } func binomial<T: BinaryInteger>(_ x: (n: T, k: T)) -> T { let nFac = factorial(x.n) let kFac = factorial(x.k) return nFac / (factorial(x.n - x.k) * kFac) } print("binomial(\(5), \(3)) = \(binomial((5, 3)))") print("binomial(\(20), \(11)) = \(binomial((20, 11)))")
<?php $n=5; $k=3; function factorial($val){ for($f=2;$val-1>1;$f*=$val--); return $f; } $binomial_coefficient=factorial($n)/(factorial($k)*factorial($n-$k)); echo $binomial_coefficient; ?>