Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Can you help me rewrite this code in Go instead of AutoHotKey, keeping it the same logically?
Loop, 5 { Output .= "Degree " (i := A_Index) ": " Loop, 10 Output .= MultiFact(A_Index, i) (A_Index = 10 ? "`n" : ", ") } MsgBox, % Output MultiFact(n, d) { Result := n while 1 < n -= d Result *= n return, Result }
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 AutoHotKey block to Go, preserving its control flow and logic.
Loop, 5 { Output .= "Degree " (i := A_Index) ": " Loop, 10 Output .= MultiFact(A_Index, i) (A_Index = 10 ? "`n" : ", ") } MsgBox, % Output MultiFact(n, d) { Result := n while 1 < n -= d Result *= n return, Result }
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 AWK snippet to C and keep its semantics consistent.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
#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)); } } }
Rewrite this program in C while keeping its functionality equivalent to the AWK version.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
#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)); } } }
Preserve the algorithm and functionality while converting the code from AWK to C#.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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 provided AWK code into C# while preserving the original functionality.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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)); } } }
Translate this program into C++ but keep the logic exactly as in AWK.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
#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 AWK snippet without changing its computational steps.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
#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 a version of this AWK function in Java with identical behavior.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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 Java translation of this AWK snippet without changing its computational steps.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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 AWK implementation into Python, maintaining the same output and logic.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
>>> 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 AWK code in Python.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
>>> 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] >>>
Generate a VB translation of this AWK snippet without changing its computational steps.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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
Translate this program into VB but keep the logic exactly as in AWK.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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 AWK snippet to Go and keep its semantics consistent.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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() } }
Ensure the translated Go code behaves exactly like the original AWK snippet.
BEGIN { for (k=1; k<=5; k++) { printf("degree %d:",k) for (n=1; n<=10; n++) { printf(" %d",multi_factorial(n,k)) } printf("\n") } exit(0) } function multi_factorial(n,k, r) { r = 1 for (; n>1; n-=k) { r *= n } return(r) }
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() } }
Write the same code in C as shown below in BBC_Basic.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
#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 BBC_Basic block to C, preserving its control flow and logic.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
#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 C# code behaves exactly like the original BBC_Basic snippet.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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 the snippet below in C# so it works the same as the original BBC_Basic code.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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)); } } }
Transform the following BBC_Basic implementation into C++, maintaining the same output and logic.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
#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 C++ as shown below in BBC_Basic.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
#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; }
Produce a language-to-language conversion: from BBC_Basic to Java, same semantics.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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(); } } }
Can you help me rewrite this code in Java instead of BBC_Basic, keeping it the same logically?
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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(); } } }
Port the provided BBC_Basic code into Python while preserving the original functionality.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
>>> 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 BBC_Basic to Python, same semantics.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
>>> 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] >>>
Generate an equivalent VB version of this BBC_Basic code.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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 BBC_Basic implementation into VB, maintaining the same output and logic.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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 language-to-language conversion: from BBC_Basic to Go, same semantics.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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() } }
Ensure the translated Go code behaves exactly like the original BBC_Basic snippet.
FOR i% = 1 TO 5 PRINT "Degree "; i%; ":"; FOR j% = 1 TO 10 PRINT " ";FNmultifact(j%, i%); NEXT PRINT NEXT END : DEF FNmultifact(n%, degree%) LOCAL i%, mfact% mfact% = 1 FOR i% = n% TO 1 STEP -degree% mfact% = mfact% * i% NEXT = mfact%
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 Clojure snippet to C and keep its semantics consistent.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
#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 Clojure implementation into C, maintaining the same output and logic.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
#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 Clojure snippet to C# and keep its semantics consistent.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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 the following code from Clojure to C#, ensuring the logic remains intact.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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 Clojure snippet to C++ and keep its semantics consistent.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
#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; }
Change the programming language of this snippet from Clojure to C++ without modifying what it does.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
#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 Clojure.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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 a version of this Clojure function in Java with identical behavior.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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(); } } }
Translate this program into Python but keep the logic exactly as in Clojure.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
>>> 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 Clojure to Python, same semantics.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
>>> 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] >>>
Generate an equivalent VB version of this Clojure code.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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
Port the following code from Clojure to VB with equivalent syntax and logic.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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
Change the following Clojure code into Go without altering its purpose.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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() } }
Produce a language-to-language conversion: from Clojure to Go, same semantics.
(defn !! [m n] (->> (iterate #(- % m) n) (take-while pos?) (apply *))) (doseq [m (range 1 6)] (prn m (map #(!! m %) (range 1 11))))
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 C version of this Common_Lisp code.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
#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 a version of this Common_Lisp function in C with identical behavior.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
#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 Common_Lisp to C# with equivalent syntax and logic.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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 Common_Lisp to C#, same semantics.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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 Common_Lisp block to C++, preserving its control flow and logic.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
#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 a version of this Common_Lisp function in C++ with identical behavior.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
#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 Common_Lisp implementation.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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(); } } }
Convert this Common_Lisp block to Java, preserving its control flow and logic.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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 Common_Lisp version.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
>>> 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 Common_Lisp to Python.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
>>> 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] >>>
Ensure the translated VB code behaves exactly like the original Common_Lisp snippet.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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
Can you help me rewrite this code in VB instead of Common_Lisp, keeping it the same logically?
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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 Common_Lisp code.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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 Common_Lisp code into Go while preserving the original functionality.
(defun mfac (n m) (reduce #'* (loop for i from n downto 1 by m collect i))) (loop for i from 1 to 10 do (format t "~2@a: ~{~a~^ ~}~%" i (loop for j from 1 to 10 collect (mfac j i))))
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() } }
Preserve the algorithm and functionality while converting the code from D to C.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
#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 D snippet to C and keep its semantics consistent.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
#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)); } } }
Keep all operations the same but rewrite the snippet in C#.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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 D to C# without modifying what it does.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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 provided D code into C++ while preserving the original functionality.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
#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 C++.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
#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; }
Transform the following D implementation into Java, maintaining the same output and logic.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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(); } } }
Convert this D snippet to Java and keep its semantics consistent.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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 D implementation into Python, maintaining the same output and logic.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
>>> 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 D code in Python.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
>>> 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 the snippet below in VB so it works the same as the original D code.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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 D version.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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 D to Go, ensuring the logic remains intact.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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.
import std.stdio, std.algorithm, std.range; T multifactorial(T=long)(in int n, in int m) pure { T one = 1; return reduce!q{a * b}(one, iota(n, 0, -m)); } void main() { foreach (immutable m; 1 .. 11) writefln("%2d: %s", m, iota(1, 11) .map!(n => multifactorial(n, m))); }
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() } }
Produce a functionally identical C code for the snippet given in Elixir.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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 the given Elixir code snippet into C without altering its behavior.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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)); } } }
Write the same code in C# as shown below in Elixir.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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 Elixir to C# without modifying what it does.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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)); } } }
Rewrite the snippet below in C++ so it works the same as the original Elixir code.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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; }
Rewrite the snippet below in C++ so it works the same as the original Elixir code.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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 Elixir snippet to Java and keep its semantics consistent.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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 following Elixir code into Java without altering its purpose.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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(); } } }
Rewrite this program in Python while keeping its functionality equivalent to the Elixir version.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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] >>>
Ensure the translated Python code behaves exactly like the original Elixir snippet.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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] >>>
Ensure the translated VB code behaves exactly like the original Elixir snippet.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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 the following code from Elixir to VB, ensuring the logic remains intact.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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
Ensure the translated Go code behaves exactly like the original Elixir snippet.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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() } }
Write the same code in Go as shown below in Elixir.
defmodule RC do def multifactorial(n,d) do Enum.take_every(n..1, d) |> Enum.reduce(1, fn x,p -> x*p end) end end Enum.each(1..5, fn d -> multifac = for n <- 1..10, do: RC.multifactorial(n,d) IO.puts "Degree 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() } }
Rewrite the snippet below in C so it works the same as the original Erlang code.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
#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)); } } }
Can you help me rewrite this code in C instead of Erlang, keeping it the same logically?
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
#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)); } } }
Change the following Erlang code into C# without altering its purpose.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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 Erlang code in C++.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
#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; }
Change the following Erlang code into C++ without altering its purpose.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
#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 Erlang block to Java, preserving its control flow and logic.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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(); } } }
Convert this Erlang block to Python, preserving its control flow and logic.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
>>> 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 the snippet below in Python so it works the same as the original Erlang code.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
>>> 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 Erlang implementation.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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
Can you help me rewrite this code in VB instead of Erlang, keeping it the same logically?
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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
Write a version of this Erlang function in Go with identical behavior.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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 Erlang code in Go.
-module(multifac). -compile(export_all). multifac(N,D) -> lists:foldl(fun (X,P) -> X * P end, 1, lists:seq(N,1,-D)). main() -> Ds = lists:seq(1,5), Ns = lists:seq(1,10), lists:foreach(fun (D) -> io:format("Degree ~b: ~p~n",[D, [ multifac(N,D) || N <- Ns]]) end, Ds).
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() } }
Write the same code in C as shown below 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
#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)); } } }