Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Can you help me rewrite this code in C instead of F#, keeping it the same logically?
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Port the following code from F# to C# with equivalent syntax and logic.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Maintain the same structure and functionality when rewriting this code in C#.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Port the following code from F# to C++ with equivalent syntax and logic.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Ensure the translated C++ code behaves exactly like the original F# snippet.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Translate this program into Java but keep the logic exactly as in F#.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Please provide an equivalent version of this F# code in Java.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Write the same algorithm in Python as shown in this F# implementation.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Maintain the same structure and functionality when rewriting this code in Python.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Please provide an equivalent version of this F# code in VB.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Convert the following code from F# to VB, ensuring the logic remains intact.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Keep all operations the same but rewrite the snippet in Go.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Keep all operations the same but rewrite the snippet in Go.
let rec mfact d = function | n when n <= d -> n | n -> n * mfact d (n-d) [<EntryPoint>] let main argv = let (|UInt|_|) = System.UInt32.TryParse >> function | true, v -> Some v | false, _ -> None let (maxDegree, maxN) = match argv with | [| UInt d; UInt n |] -> (int d, int n) | [| UInt d |] -> (int d, 10) | _ -> (5, 10) let showFor d = List.init maxN (fun i -> mfact d (i+1)) |> printfn "%i: %A" d ignore (List.init maxDegree (fun i -> showFor (i+1))) 0
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Port the provided Fortran code into C# while preserving the original functionality.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Convert this Fortran block to C#, preserving its control flow and logic.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Produce a language-to-language conversion: from Fortran to C++, same semantics.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Generate an equivalent C++ version of this Fortran code.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Rewrite this program in C while keeping its functionality equivalent to the Fortran version.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Translate the given Fortran code snippet into C without altering its behavior.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Ensure the translated Java code behaves exactly like the original Fortran snippet.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Write the same algorithm in Java as shown in this Fortran implementation.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Rewrite this program in Python while keeping its functionality equivalent to the Fortran version.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Convert this Fortran block to Python, preserving its control flow and logic.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Rewrite this program in VB while keeping its functionality equivalent to the Fortran version.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Convert this Fortran snippet to VB and keep its semantics consistent.
program test implicit none integer :: i, j, n do i = 1, 5 write(*, "(a, i0, a)", advance = "no") "Degree ", i, ": " do j = 1, 10 n = multifactorial(j, i) write(*, "(i0, 1x)", advance = "no") n end do write(*,*) end do contains function multifactorial (range, degree) integer :: multifactorial, range, degree integer :: k multifactorial = product((/(k, k=range, 1, -degree)/)) end function multifactorial end program test
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Keep all operations the same but rewrite the snippet in C.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Produce a functionally identical C code for the snippet given in Haskell.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Write the same algorithm in C# as shown in this Haskell implementation.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Can you help me rewrite this code in C# instead of Haskell, keeping it the same logically?
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Produce a functionally identical C++ code for the snippet given in Haskell.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Generate an equivalent C++ version of this Haskell code.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Keep all operations the same but rewrite the snippet in Java.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Rewrite the snippet below in Java so it works the same as the original Haskell code.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Transform the following Haskell implementation into Python, maintaining the same output and logic.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Port the provided Haskell code into Python while preserving the original functionality.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Preserve the algorithm and functionality while converting the code from Haskell to VB.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Ensure the translated VB code behaves exactly like the original Haskell snippet.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Produce a functionally identical Go code for the snippet given in Haskell.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Convert this Haskell snippet to Go and keep its semantics consistent.
mulfac :: (Num a, Enum a) => a -> [a] mulfac k = 1 : s where s = [1 .. k] <> zipWith (*) s [k + 1 ..] mulfac1 :: (Num a, Enum a) => a -> a -> a mulfac1 k n = product [n, n - k .. 1] main :: IO () main = mapM_ (print . take 10 . tail . mulfac) [1 .. 5]
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Rewrite this program in C while keeping its functionality equivalent to the J version.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Write the same algorithm in C as shown in this J implementation.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Generate an equivalent C# version of this J code.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Please provide an equivalent version of this J code in C#.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Change the programming language of this snippet from J to C++ without modifying what it does.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Generate a C++ translation of this J snippet without changing its computational steps.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Write the same algorithm in Java as shown in this J implementation.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Produce a language-to-language conversion: from J to Java, same semantics.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Produce a language-to-language conversion: from J to Python, same semantics.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Keep all operations the same but rewrite the snippet in Python.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Write the same algorithm in VB as shown in this J implementation.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Generate a VB translation of this J snippet without changing its computational steps.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Generate an equivalent Go version of this J code.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Please provide an equivalent version of this J code in Go.
multifact=: */@([ - ] * i.@>.@%)&> ('';' degree'),multifact table >:i.10 ┌─────────┬──────────────────────────────────────┐ │ │ degree │ ├─────────┼──────────────────────────────────────┤ │multifact│ 1 2 3 4 5 6 7 8 9 10│ ├─────────┼──────────────────────────────────────┤ │ 1 │ 1 1 1 1 1 1 1 1 1 1│ │ 2 │ 2 2 2 2 2 2 2 2 2 2│ │ 3 │ 6 3 3 3 3 3 3 3 3 3│ │ 4 │ 24 8 4 4 4 4 4 4 4 4│ │ 5 │ 120 15 10 5 5 5 5 5 5 5│ │ 6 │ 720 48 18 12 6 6 6 6 6 6│ │ 7 │ 5040 105 28 21 14 7 7 7 7 7│ │ 8 │ 40320 384 80 32 24 16 8 8 8 8│ │ 9 │ 362880 945 162 45 36 27 18 9 9 9│ │10 │3628800 3840 280 120 50 40 30 20 10 10│ └─────────┴──────────────────────────────────────┘
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Rewrite this program in C while keeping its functionality equivalent to the Julia version.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Generate an equivalent C version of this Julia code.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Transform the following Julia implementation into C#, maintaining the same output and logic.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Change the programming language of this snippet from Julia to C# without modifying what it does.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Produce a functionally identical C++ code for the snippet given in Julia.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Convert this Julia block to C++, preserving its control flow and logic.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Translate this program into Java but keep the logic exactly as in Julia.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Maintain the same structure and functionality when rewriting this code in Java.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Produce a functionally identical Python code for the snippet given in Julia.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Produce a language-to-language conversion: from Julia to Python, same semantics.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Port the following code from Julia to VB with equivalent syntax and logic.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Rewrite this program in VB while keeping its functionality equivalent to the Julia version.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Maintain the same structure and functionality when rewriting this code in Go.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Port the provided Julia code into Go while preserving the original functionality.
using Printf function multifact(n::Integer, k::Integer) n > 0 && k > 0 || throw(DomainError()) k > 1 || factorial(n) return prod(n:-k:2) end const khi = 5 const nhi = 10 println("Showing multifactorial for n in [1, $nhi] and k in [1, $khi].") for k = 1:khi a = multifact.(1:nhi, k) lab = "n" * "!" ^ k @printf(" %-6s → %s\n", lab, a) end
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Convert this Lua block to C, preserving its control flow and logic.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Convert this Lua snippet to C and keep its semantics consistent.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Translate this program into C# but keep the logic exactly as in Lua.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Ensure the translated C# code behaves exactly like the original Lua snippet.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Write a version of this Lua function in C++ with identical behavior.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Convert the following code from Lua to C++, ensuring the logic remains intact.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Maintain the same structure and functionality when rewriting this code in Java.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Keep all operations the same but rewrite the snippet in Java.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Change the programming language of this snippet from Lua to Python without modifying what it does.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Produce a language-to-language conversion: from Lua to Python, same semantics.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Maintain the same structure and functionality when rewriting this code in VB.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Convert this Lua snippet to VB and keep its semantics consistent.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Produce a functionally identical Go code for the snippet given in Lua.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Keep all operations the same but rewrite the snippet in Go.
function multiFact (n, degree) local fact = 1 for i = n, 2, -degree do fact = fact * i end return fact end print("Degree\t|\tMultifactorials 1 to 10") print(string.rep("-", 52)) for d = 1, 5 do io.write(" " .. d, "\t| ") for n = 1, 10 do io.write(multiFact(n, d) .. " ") end print() end
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Generate a C translation of this Mathematica snippet without changing its computational steps.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Produce a functionally identical C code for the snippet given in Mathematica.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Write the same code in C# as shown below in Mathematica.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Rewrite this program in C# while keeping its functionality equivalent to the Mathematica version.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Ensure the translated C++ code behaves exactly like the original Mathematica snippet.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Keep all operations the same but rewrite the snippet in C++.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }
Write the same code in Java as shown below in Mathematica.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Produce a language-to-language conversion: from Mathematica to Java, same semantics.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
public class MultiFact { private static long multiFact(long n, int deg){ long ans = 1; for(long i = n; i > 0; i -= deg){ ans *= i; } return ans; } public static void main(String[] args){ for(int deg = 1; deg <= 5; deg++){ System.out.print("degree " + deg + ":"); for(long n = 1; n <= 10; n++){ System.out.print(" " + multiFact(n, deg)); } System.out.println(); } } }
Generate a Python translation of this Mathematica snippet without changing its computational steps.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Please provide an equivalent version of this Mathematica code in Python.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
>>> from functools import reduce >>> from operator import mul >>> def mfac(n, m): return reduce(mul, range(n, 0, -m)) >>> for m in range(1, 11): print("%2i: %r" % (m, [mfac(n, m) for n in range(1, 11)])) 1: [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800] 2: [1, 2, 3, 8, 15, 48, 105, 384, 945, 3840] 3: [1, 2, 3, 4, 10, 18, 28, 80, 162, 280] 4: [1, 2, 3, 4, 5, 12, 21, 32, 45, 120] 5: [1, 2, 3, 4, 5, 6, 14, 24, 36, 50] 6: [1, 2, 3, 4, 5, 6, 7, 16, 27, 40] 7: [1, 2, 3, 4, 5, 6, 7, 8, 18, 30] 8: [1, 2, 3, 4, 5, 6, 7, 8, 9, 20] 9: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 10: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>>
Write the same algorithm in VB as shown in this Mathematica implementation.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Transform the following Mathematica implementation into VB, maintaining the same output and logic.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
Function multifactorial(n,d) If n = 0 Then multifactorial = 1 Else For i = n To 1 Step -d If i = n Then multifactorial = n Else multifactorial = multifactorial * i End If Next End If End Function For j = 1 To 5 WScript.StdOut.Write "Degree " & j & ": " For k = 1 To 10 If k = 10 Then WScript.StdOut.Write multifactorial(k,j) Else WScript.StdOut.Write multifactorial(k,j) & " " End If Next WScript.StdOut.WriteLine Next
Rewrite the snippet below in Go so it works the same as the original Mathematica code.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Generate an equivalent Go version of this Mathematica code.
Multifactorial[n_, d_] := Product[x, {x, n, 1, -d}] Table[Multifactorial[j, i], {i, 5}, {j, 10}]//TableForm
package main import "fmt" func multiFactorial(n, k int) int { r := 1 for ; n > 1; n -= k { r *= n } return r } func main() { for k := 1; k <= 5; k++ { fmt.Print("degree ", k, ":") for n := 1; n <= 10; n++ { fmt.Print(" ", multiFactorial(n, k)) } fmt.Println() } }
Change the following Nim code into C without altering its purpose.
proc multifact(n, deg: int): int = result = (if n <= deg: n else: n * multifact(n - deg, deg)) proc multifactI(n, deg: int): int = result = n var n = n while n >= deg + 1: result *= n - deg n -= deg for i in 1..5: stdout.write "Degree ", i, ": " for j in 1..10: stdout.write multifactI(j, i), " " stdout.write('\n')
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Please provide an equivalent version of this Nim code in C.
proc multifact(n, deg: int): int = result = (if n <= deg: n else: n * multifact(n - deg, deg)) proc multifactI(n, deg: int): int = result = n var n = n while n >= deg + 1: result *= n - deg n -= deg for i in 1..5: stdout.write "Degree ", i, ": " for j in 1..10: stdout.write multifactI(j, i), " " stdout.write('\n')
#include <stdio.h> #define HIGHEST_DEGREE 5 #define LARGEST_NUMBER 10 int multifact(int n, int deg){ return n <= deg ? n : n * multifact(n - deg, deg); } int multifact_i(int n, int deg){ int result = n; while (n >= deg + 1){ result *= (n - deg); n -= deg; } return result; } int main(void){ int i, j; for (i = 1; i <= HIGHEST_DEGREE; i++){ printf("\nDegree %d: ", i); for (j = 1; j <= LARGEST_NUMBER; j++){ printf("%d ", multifact(j, i)); } } }
Generate an equivalent C# version of this Nim code.
proc multifact(n, deg: int): int = result = (if n <= deg: n else: n * multifact(n - deg, deg)) proc multifactI(n, deg: int): int = result = n var n = n while n >= deg + 1: result *= n - deg n -= deg for i in 1..5: stdout.write "Degree ", i, ": " for j in 1..10: stdout.write multifactI(j, i), " " stdout.write('\n')
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Please provide an equivalent version of this Nim code in C#.
proc multifact(n, deg: int): int = result = (if n <= deg: n else: n * multifact(n - deg, deg)) proc multifactI(n, deg: int): int = result = n var n = n while n >= deg + 1: result *= n - deg n -= deg for i in 1..5: stdout.write "Degree ", i, ": " for j in 1..10: stdout.write multifactI(j, i), " " stdout.write('\n')
namespace RosettaCode.Multifactorial { using System; using System.Linq; internal static class Program { private static void Main() { Console.WriteLine(string.Join(Environment.NewLine, Enumerable.Range(1, 5) .Select( degree => string.Join(" ", Enumerable.Range(1, 10) .Select( number => Multifactorial(number, degree)))))); } private static int Multifactorial(int number, int degree) { if (degree < 1) { throw new ArgumentOutOfRangeException("degree"); } var count = 1 + (number - 1) / degree; if (count < 1) { throw new ArgumentOutOfRangeException("number"); } return Enumerable.Range(0, count) .Aggregate(1, (accumulator, index) => accumulator * (number - degree * index)); } } }
Ensure the translated C++ code behaves exactly like the original Nim snippet.
proc multifact(n, deg: int): int = result = (if n <= deg: n else: n * multifact(n - deg, deg)) proc multifactI(n, deg: int): int = result = n var n = n while n >= deg + 1: result *= n - deg n -= deg for i in 1..5: stdout.write "Degree ", i, ": " for j in 1..10: stdout.write multifactI(j, i), " " stdout.write('\n')
#include <algorithm> #include <iostream> #include <iterator> int main(void) { for (int g = 1; g < 10; g++) { int v[11], n=0; generate_n(std::ostream_iterator<int>(std::cout, " "), 10, [&]{n++; return v[n]=(g<n)? v[n-g]*n : n;}); std::cout << std::endl; } return 0; }