Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Write the same code in Python as shown below in Mathematica.
w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list]; While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s] m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Convert this Mathematica block to Go, preserving its control flow and logic.
w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list]; While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s] m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Change the following Nim code into C without altering its purpose.
import algorithm, math, sequtils, strutils type Partition = seq[seq[int]] func isIncreasing(s: seq[int]): bool = var prev = 0 for val in s: if prev >= val: return false prev = val result = true iterator partitions(lengths: varargs[int]): Partition = var slices: seq[Slice[int]] var delta...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Can you help me rewrite this code in C# instead of Nim, keeping it the same logically?
import algorithm, math, sequtils, strutils type Partition = seq[seq[int]] func isIncreasing(s: seq[int]): bool = var prev = 0 for val in s: if prev >= val: return false prev = val result = true iterator partitions(lengths: varargs[int]): Partition = var slices: seq[Slice[int]] var delta...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Change the programming language of this snippet from Nim to C++ without modifying what it does.
import algorithm, math, sequtils, strutils type Partition = seq[seq[int]] func isIncreasing(s: seq[int]): bool = var prev = 0 for val in s: if prev >= val: return false prev = val result = true iterator partitions(lengths: varargs[int]): Partition = var slices: seq[Slice[int]] var delta...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Convert this Nim block to Python, preserving its control flow and logic.
import algorithm, math, sequtils, strutils type Partition = seq[seq[int]] func isIncreasing(s: seq[int]): bool = var prev = 0 for val in s: if prev >= val: return false prev = val result = true iterator partitions(lengths: varargs[int]): Partition = var slices: seq[Slice[int]] var delta...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Write the same code in Go as shown below in Nim.
import algorithm, math, sequtils, strutils type Partition = seq[seq[int]] func isIncreasing(s: seq[int]): bool = var prev = 0 for val in s: if prev >= val: return false prev = val result = true iterator partitions(lengths: varargs[int]): Partition = var slices: seq[Slice[int]] var delta...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Produce a functionally identical C code for the snippet given in Perl.
use strict; use warnings; use Thread 'async'; use Thread::Queue; sub make_slices { my ($n, @avail) = (shift, @{ +shift }); my ($q, @part, $gen); $gen = sub { my $pos = shift; if (@part == $n) { ...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Produce a language-to-language conversion: from Perl to C#, same semantics.
use strict; use warnings; use Thread 'async'; use Thread::Queue; sub make_slices { my ($n, @avail) = (shift, @{ +shift }); my ($q, @part, $gen); $gen = sub { my $pos = shift; if (@part == $n) { ...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Please provide an equivalent version of this Perl code in C++.
use strict; use warnings; use Thread 'async'; use Thread::Queue; sub make_slices { my ($n, @avail) = (shift, @{ +shift }); my ($q, @part, $gen); $gen = sub { my $pos = shift; if (@part == $n) { ...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Generate an equivalent Python version of this Perl code.
use strict; use warnings; use Thread 'async'; use Thread::Queue; sub make_slices { my ($n, @avail) = (shift, @{ +shift }); my ($q, @part, $gen); $gen = sub { my $pos = shift; if (@part == $n) { ...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Produce a functionally identical Go code for the snippet given in Perl.
use strict; use warnings; use Thread 'async'; use Thread::Queue; sub make_slices { my ($n, @avail) = (shift, @{ +shift }); my ($q, @part, $gen); $gen = sub { my $pos = shift; if (@part == $n) { ...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Generate a C translation of this Racket snippet without changing its computational steps.
#lang racket (define (comb k xs) (cond [(zero? k) (list (cons '() xs))] [(null? xs) '()] [else (append (for/list ([cszs (comb (sub1 k) (cdr xs))]) (cons (cons (car xs) (car cszs)) (cdr cszs))) (for/list ([cszs (comb k (cdr xs))]) (...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Convert the following code from Racket to C#, ensuring the logic remains intact.
#lang racket (define (comb k xs) (cond [(zero? k) (list (cons '() xs))] [(null? xs) '()] [else (append (for/list ([cszs (comb (sub1 k) (cdr xs))]) (cons (cons (car xs) (car cszs)) (cdr cszs))) (for/list ([cszs (comb k (cdr xs))]) (...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Produce a functionally identical C++ code for the snippet given in Racket.
#lang racket (define (comb k xs) (cond [(zero? k) (list (cons '() xs))] [(null? xs) '()] [else (append (for/list ([cszs (comb (sub1 k) (cdr xs))]) (cons (cons (car xs) (car cszs)) (cdr cszs))) (for/list ([cszs (comb k (cdr xs))]) (...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Generate an equivalent Python version of this Racket code.
#lang racket (define (comb k xs) (cond [(zero? k) (list (cons '() xs))] [(null? xs) '()] [else (append (for/list ([cszs (comb (sub1 k) (cdr xs))]) (cons (cons (car xs) (car cszs)) (cdr cszs))) (for/list ([cszs (comb k (cdr xs))]) (...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Port the following code from Racket to Go with equivalent syntax and logic.
#lang racket (define (comb k xs) (cond [(zero? k) (list (cons '() xs))] [(null? xs) '()] [else (append (for/list ([cszs (comb (sub1 k) (cdr xs))]) (cons (cons (car xs) (car cszs)) (cdr cszs))) (for/list ([cszs (comb k (cdr xs))]) (...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Ensure the translated C code behaves exactly like the original REXX snippet.
/ call orderedPartitions 2,0,2 call orderedPartitions 1,1,1 call orderedPartitions 1,2,0,1 exit orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 t=0 ...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Generate an equivalent C# version of this REXX code.
/ call orderedPartitions 2,0,2 call orderedPartitions 1,1,1 call orderedPartitions 1,2,0,1 exit orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 t=0 ...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Write the same code in C++ as shown below in REXX.
/ call orderedPartitions 2,0,2 call orderedPartitions 1,1,1 call orderedPartitions 1,2,0,1 exit orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 t=0 ...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Convert this REXX snippet to Python and keep its semantics consistent.
/ call orderedPartitions 2,0,2 call orderedPartitions 1,1,1 call orderedPartitions 1,2,0,1 exit orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 t=0 ...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Transform the following REXX implementation into Go, maintaining the same output and logic.
/ call orderedPartitions 2,0,2 call orderedPartitions 1,1,1 call orderedPartitions 1,2,0,1 exit orderedPartitions: procedure; #=arg(); bot.=; top.=; low=; high=; d=123456789 t=0 ...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Convert this Ruby snippet to C and keep its semantics consistent.
def partition(mask) return [[]] if mask.empty? [*1..mask.inject(:+)].permutation.map {|perm| mask.map {|num_elts| perm.shift(num_elts).sort } }.uniq end
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Can you help me rewrite this code in C# instead of Ruby, keeping it the same logically?
def partition(mask) return [[]] if mask.empty? [*1..mask.inject(:+)].permutation.map {|perm| mask.map {|num_elts| perm.shift(num_elts).sort } }.uniq end
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Change the programming language of this snippet from Ruby to C++ without modifying what it does.
def partition(mask) return [[]] if mask.empty? [*1..mask.inject(:+)].permutation.map {|perm| mask.map {|num_elts| perm.shift(num_elts).sort } }.uniq end
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Generate a Python translation of this Ruby snippet without changing its computational steps.
def partition(mask) return [[]] if mask.empty? [*1..mask.inject(:+)].permutation.map {|perm| mask.map {|num_elts| perm.shift(num_elts).sort } }.uniq end
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Change the programming language of this snippet from Ruby to Go without modifying what it does.
def partition(mask) return [[]] if mask.empty? [*1..mask.inject(:+)].permutation.map {|perm| mask.map {|num_elts| perm.shift(num_elts).sort } }.uniq end
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Transform the following Scala implementation into C, maintaining the same output and logic.
fun nextPerm(perm: IntArray): Boolean { val size = perm.size var k = -1 for (i in size - 2 downTo 0) { if (perm[i] < perm[i + 1]) { k = i break } } if (k == -1) return false for (l in size - 1 downTo k) { if (perm[k] < perm[l]) { va...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Rewrite this program in C# while keeping its functionality equivalent to the Scala version.
fun nextPerm(perm: IntArray): Boolean { val size = perm.size var k = -1 for (i in size - 2 downTo 0) { if (perm[i] < perm[i + 1]) { k = i break } } if (k == -1) return false for (l in size - 1 downTo k) { if (perm[k] < perm[l]) { va...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Port the following code from Scala to C++ with equivalent syntax and logic.
fun nextPerm(perm: IntArray): Boolean { val size = perm.size var k = -1 for (i in size - 2 downTo 0) { if (perm[i] < perm[i + 1]) { k = i break } } if (k == -1) return false for (l in size - 1 downTo k) { if (perm[k] < perm[l]) { va...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Change the programming language of this snippet from Scala to Python without modifying what it does.
fun nextPerm(perm: IntArray): Boolean { val size = perm.size var k = -1 for (i in size - 2 downTo 0) { if (perm[i] < perm[i + 1]) { k = i break } } if (k == -1) return false for (l in size - 1 downTo k) { if (perm[k] < perm[l]) { va...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Port the following code from Scala to Go with equivalent syntax and logic.
fun nextPerm(perm: IntArray): Boolean { val size = perm.size var k = -1 for (i in size - 2 downTo 0) { if (perm[i] < perm[i + 1]) { k = i break } } if (k == -1) return false for (l in size - 1 downTo k) { if (perm[k] < perm[l]) { va...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Generate an equivalent C version of this Tcl code.
package require Tcl 8.5 package require struct::set proc selectCombinationsFrom {k l} { if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]} set all {} set n [expr {[llength $l] - [incr k -1]}] for {set i 0} {$i < $n} {} { set first [lindex $l $i] incr i if {$k == ...
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
Port the provided Tcl code into C# while preserving the original functionality.
package require Tcl 8.5 package require struct::set proc selectCombinationsFrom {k l} { if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]} set all {} set n [expr {[llength $l] - [incr k -1]}] for {set i 0} {$i < $n} {} { set first [lindex $l $i] incr i if {$k == ...
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
Change the programming language of this snippet from Tcl to C++ without modifying what it does.
package require Tcl 8.5 package require struct::set proc selectCombinationsFrom {k l} { if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]} set all {} set n [expr {[llength $l] - [incr k -1]}] for {set i 0} {$i < $n} {} { set first [lindex $l $i] incr i if {$k == ...
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
Generate a Python translation of this Tcl snippet without changing its computational steps.
package require Tcl 8.5 package require struct::set proc selectCombinationsFrom {k l} { if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]} set all {} set n [expr {[llength $l] - [incr k -1]}] for {set i 0} {$i < $n} {} { set first [lindex $l $i] incr i if {$k == ...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Produce a functionally identical Go code for the snippet given in Tcl.
package require Tcl 8.5 package require struct::set proc selectCombinationsFrom {k l} { if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]} set all {} set n [expr {[llength $l] - [incr k -1]}] for {set i 0} {$i < $n} {} { set first [lindex $l $i] incr i if {$k == ...
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
Convert this C block to Rust, preserving its control flow and logic.
#include <stdio.h> int next_perm(int size, int * nums) { int *l, *k, tmp; for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {}; if (k < nums) return 0; for (l = nums + size - 1; *l <= *k; l--) {}; tmp = *k; *k = *l; *l = tmp; for (l = nums + size - 1, k++; k <...
use itertools::Itertools; type NArray = Vec<Vec<Vec<usize>>>; fn generate_partitions(args: &[usize]) -> NArray { let max = args.iter().sum(); let c = args.iter().fold(vec![], |mut acc, arg| { acc.push((1..=max).combinations(*arg).collect::<Vec<_>>()); acc }); ...
Ensure the translated Rust code behaves exactly like the original C# snippet.
using System; using System.Linq; using System.Collections.Generic; public static class OrderedPartitions { public static void Main() { var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } }; foreach (int[] sizes in input) { foreach (var partition in Partiti...
use itertools::Itertools; type NArray = Vec<Vec<Vec<usize>>>; fn generate_partitions(args: &[usize]) -> NArray { let max = args.iter().sum(); let c = args.iter().fold(vec![], |mut acc, arg| { acc.push((1..=max).combinations(*arg).collect::<Vec<_>>()); acc }); ...
Convert this Go block to Rust, preserving its control flow and logic.
package main import ( "fmt" "os" "strconv" ) func gen_part(n, res []int, pos int) { if pos == len(res) { x := make([][]int, len(n)) for i, c := range res { x[c] = append(x[c], i+1) } fmt.Println(x) return } for i := range n { if n[i] == 0 { continue } n[i], res[pos] = n[i]-1, i gen_par...
use itertools::Itertools; type NArray = Vec<Vec<Vec<usize>>>; fn generate_partitions(args: &[usize]) -> NArray { let max = args.iter().sum(); let c = args.iter().fold(vec![], |mut acc, arg| { acc.push((1..=max).combinations(*arg).collect::<Vec<_>>()); acc }); ...
Rewrite the snippet below in Python so it works the same as the original Rust code.
use itertools::Itertools; type NArray = Vec<Vec<Vec<usize>>>; fn generate_partitions(args: &[usize]) -> NArray { let max = args.iter().sum(); let c = args.iter().fold(vec![], |mut acc, arg| { acc.push((1..=max).combinations(*arg).collect::<Vec<_>>()); acc }); ...
from itertools import combinations def partitions(*args): def p(s, *args): if not args: return [[]] res = [] for c in combinations(s, args[0]): s0 = [x for x in s if x not in c] for r in p(s0, *args[1:]): res.append([c] + r) return res s =...
Change the following C++ code into Rust without altering its purpose.
#include <iostream> #include <algorithm> #include <vector> #include <numeric> void partitions(std::vector<size_t> args) { size_t sum = std::accumulate(std::begin(args), std::end(args), 0); std::vector<size_t> nums(sum); std::iota(std::begin(nums), std::end(nums), 1); do { size_t total_index = 0; std::v...
use itertools::Itertools; type NArray = Vec<Vec<Vec<usize>>>; fn generate_partitions(args: &[usize]) -> NArray { let max = args.iter().sum(); let c = args.iter().fold(vec![], |mut acc, arg| { acc.push((1..=max).combinations(*arg).collect::<Vec<_>>()); acc }); ...
Write the same algorithm in C# as shown in this Ada implementation.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Ensure the translated C code behaves exactly like the original Ada snippet.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Change the following Ada code into C++ without altering its purpose.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) return true; else { mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) ...
Ensure the translated Go code behaves exactly like the original Ada snippet.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Maintain the same structure and functionality when rewriting this code in Java.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Translate this program into Python but keep the logic exactly as in Ada.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Generate an equivalent VB version of this Ada code.
with Ada.Text_Io; use Ada.Text_Io; with Ada.Integer_Text_Io; use Ada.Integer_Text_Io; procedure Lucas_Lehmer_Test is type Ull is mod 2**64; function Mersenne(Item : Integer) return Boolean is S : Ull := 4; MP : Ull := 2**Item - 1; begin if Item = 2 then return True; else ...
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Generate an equivalent C version of this Arturo code.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Write the same code in C# as shown below in Arturo.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Write the same code in C++ as shown below in Arturo.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Change the following Arturo code into Java without altering its purpose.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Generate an equivalent Python version of this Arturo code.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Maintain the same structure and functionality when rewriting this code in VB.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Convert this Arturo snippet to Go and keep its semantics consistent.
mersenne?: function [p][ if p=2 -> return true mp: dec shl 1 p s: 4 loop 3..p 'i -> s: (sub s*s 2) % mp return s=0 ] print "Mersenne primes:" mersennes: select 2..32 'x -> and? prime? x mersenne? x print join.with:", " map mersennes 'm -> ~"M|m|"
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Transform the following AWK implementation into C, maintaining the same output and logic.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Generate an equivalent C# version of this AWK code.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Port the following code from AWK to C++ with equivalent syntax and logic.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Generate an equivalent Java version of this AWK code.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Translate the given AWK code snippet into Python without altering its behavior.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Produce a language-to-language conversion: from AWK to VB, same semantics.
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Can you help me rewrite this code in Go instead of AWK, keeping it the same logically?
BEGIN { printf("Mersenne primes:") n = 1 for (exponent=2; exponent<=32; exponent++) { s = (exponent == 2) ? 0 : 4 n = (n+1)*2-1 for (i=1; i<=exponent-2; i++) { s = (s*s-2)%n } if (s == 0) { printf(" M%s",exponent) } } printf("\n") exit(0) }
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Rewrite the snippet below in C so it works the same as the original BBC_Basic code.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Translate this program into C# but keep the logic exactly as in BBC_Basic.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Write the same algorithm in C++ as shown in this BBC_Basic implementation.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Produce a language-to-language conversion: from BBC_Basic to Java, same semantics.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Produce a functionally identical Python code for the snippet given in BBC_Basic.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Can you help me rewrite this code in VB instead of BBC_Basic, keeping it the same logically?
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Generate an equivalent Go version of this BBC_Basic code.
*FLOAT 64 PRINT "Mersenne Primes:" FOR p% = 2 TO 23 IF FNlucas_lehmer(p%) PRINT "M" ; p% NEXT END DEF FNlucas_lehmer(p%) LOCAL i%, mp, sn IF p% = 2 THEN = TRUE IF (p% AND 1) = 0 THEN = FALSE mp = 2^p% - 1 sn = 4 FOR i% = 3 TO p% ...
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Change the following Common_Lisp code into C without altering its purpose.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Generate a C# translation of this Common_Lisp snippet without changing its computational steps.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Write the same algorithm in C++ as shown in this Common_Lisp implementation.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Translate this program into Java but keep the logic exactly as in Common_Lisp.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Convert this Common_Lisp snippet to Python and keep its semantics consistent.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Ensure the translated VB code behaves exactly like the original Common_Lisp snippet.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Port the following code from Common_Lisp to Go with equivalent syntax and logic.
(defn prime? [i] (cond (< i 4) (>= i 2) (zero? (rem i 2)) false :else (not-any? #(zero? (rem i %)) (range 3 (inc (Math/sqrt i)))))))) (defn mersenne? [p] (or (= p 2) (let [mp (dec (bit-shift-left 1 p))] (loop [n 3 s 4] (if (> n p) (zero? s) (recur (inc n) (rem (- (* ...
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Rewrite this program in C while keeping its functionality equivalent to the D version.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Generate a C# translation of this D snippet without changing its computational steps.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Generate a C++ translation of this D snippet without changing its computational steps.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Produce a functionally identical Java code for the snippet given in D.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Transform the following D implementation into Python, maintaining the same output and logic.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Convert the following code from D to VB, ensuring the logic remains intact.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Transform the following D implementation into Go, maintaining the same output and logic.
import std.stdio, std.math, std.bigint; bool isPrime(in uint p) pure nothrow @safe @nogc { if (p < 2 || p % 2 == 0) return p == 2; foreach (immutable i; 3 .. cast(uint)real(p).sqrt + 1) if (p % i == 0) return false; return true; } bool isMersennePrime(in uint p) pure nothrow {...
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Write a version of this Delphi function in C with identical behavior.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Generate a C# translation of this Delphi snippet without changing its computational steps.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Generate an equivalent C++ version of this Delphi code.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Convert this Delphi snippet to Java and keep its semantics consistent.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Port the provided Delphi code into Python while preserving the original functionality.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Can you help me rewrite this code in VB instead of Delphi, keeping it the same logically?
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Keep all operations the same but rewrite the snippet in Go.
function IsMersennePrime(p : Integer) : Boolean; var i, s, m_p : Integer; begin if p=2 then Result:=True else begin m_p := (1 shl p)-1; s := 4; for i:=3 to p do s:=(s*s-2) mod m_p; Result:=(s=0); end; end; const upperBound = Round(Log2(High(Integer))/2); PrintLn('Fin...
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Can you help me rewrite this code in C instead of Elixir, keeping it the same logically?
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Change the following Elixir code into C# without altering its purpose.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...
Generate a C++ translation of this Elixir snippet without changing its computational steps.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
#include <iostream> #include <gmpxx.h> static bool is_mersenne_prime(mpz_class p) { if( 2 == p ) { return true; } mpz_class s(4); mpz_class div( (mpz_class(1) << p.get_ui()) - 1 ); for( mpz_class i(3); i <= p; ++i ) { s = (s * s - mpz_...
Preserve the algorithm and functionality while converting the code from Elixir to Java.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
import java.math.BigInteger; public class Mersenne { public static boolean isPrime(int p) { if (p == 2) return true; else if (p <= 1 || p % 2 == 0) return false; else { int to = (int)Math.sqrt(p); for (int i = 3; i <= to; i += 2) ...
Produce a language-to-language conversion: from Elixir to Python, same semantics.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
from sys import stdout from math import sqrt, log def is_prime ( p ): if p == 2: return True elif p <= 1 or p % 2 == 0: return False else: for i in range(3, int(sqrt(p))+1, 2 ): if p % i == 0: return False return True def is_mersenne_prime ( p ): if p == 2: return True else: m_p = ( ...
Port the following code from Elixir to VB with equivalent syntax and logic.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
iexpmax = 15 n=1 out="" For iexp = 2 To iexpmax If iexp = 2 Then s = 0 Else s = 4 End If n = (n + 1) * 2 - 1 For i = 1 To iexp - 2 s = (s * s - 2) Mod n Next If s = 0 Then out=out & "M" & iexp & " " End If Next Wscript.echo out
Port the provided Elixir code into Go while preserving the original functionality.
defmodule LucasLehmer do use Bitwise def test do for p <- 2..1300, p==2 or s(bsl(1,p)-1, p-1)==0, do: IO.write "M end defp s(mp, 1), do: rem(4, mp) defp s(mp, n) do x = s(mp, n-1) rem(x*x-2, mp) end end LucasLehmer.test
package main import ( "fmt" "math/big" ) var primes = []uint{3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127} var mersennes = []uint{521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 8...
Change the following Erlang code into C without altering its purpose.
-module(mp). -export([main/0]). main() -> [ io:format("M~p ", [P]) || P <- lists:seq(2,700), (P == 2) orelse (s((1 bsl P) - 1, P-1) == 0) ]. s(MP,1) -> 4 rem MP; s(MP,N) -> X=s(MP,N-1), (X*X - 2) rem MP.
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <gmp.h> int lucas_lehmer(unsigned long p) { mpz_t V, mp, t; unsigned long k, tlim; int res; if (p == 2) return 1; if (!(p&1)) return 0; mpz_init_set_ui(t, p); if (!mpz_probab_prime_p(t, 25)) { mpz_clear(t); return 0; } if (p < ...
Please provide an equivalent version of this Erlang code in C#.
-module(mp). -export([main/0]). main() -> [ io:format("M~p ", [P]) || P <- lists:seq(2,700), (P == 2) orelse (s((1 bsl P) - 1, P-1) == 0) ]. s(MP,1) -> 4 rem MP; s(MP,N) -> X=s(MP,N-1), (X*X - 2) rem MP.
using System; using System.Collections.Generic; using System.Numerics; using System.Threading.Tasks; namespace LucasLehmerTestForRosettaCode { public class LucasLehmerTest { static BigInteger ZERO = new BigInteger(0); static BigInteger ONE = new BigInteger(1); static BigInteger TWO = ne...