Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include <stdio.h>
inline int self_desc(unsigned long long xx)
{
register unsigned int d, x;
unsigned char cnt[10] = {0}, dig[10] = {0};
for (d = 0; xx > ~0U; xx /= 10)
cnt[ dig[d++] = xx % 10 ]++;
for (x = xx; x; x /= 10)
cnt[ dig[d++] = x % 10 ]++;
while(d-- && dig[x++] == cnt[d]);
return d == -1;
... | selfDescribing?: function [x][
digs: digits x
loop.with:'i digs 'd [
if d <> size select digs 'z [z=i]
-> return false
]
return true
]
print select 1..22000 => selfDescribing?
|
Port the provided C code into Arturo while preserving the original functionality. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_SIZE 32
typedef struct string_tag {
size_t length;
char str[MAX_WORD_SIZE];
} string_t;
void fatal(const char* message) {
fprintf(stderr, "%s\n", message);
exit(1);
}
void* xmalloc(size_t n) {
void* ptr = malloc(n);
... | wordset: map read.lines relative "unixdict.txt" => strip
wordset: select wordset 'word -> 12 =< size word
results: new []
loop wordset 'a [
loop select wordset 'word [equal? size a size word] 'b [
if a <> b [
if 1 = levenshtein a b [
'results ++ @[sort @[a b]]
]
... |
Translate the given C code snippet into Arturo without altering its behavior. | #include <stdio.h>
#define SIZE 256
int check(char *word) {
int e = 0, ok = 1;
for(; *word && ok; word++) {
switch(*word) {
case 'a':
case 'i':
case 'o':
case 'u': ok = 0; break;
case 'e': e++;
}
}
return ok && e > 3;
}
... | words: read.lines relative "unixdict.txt"
otherVowels: ["a" "i" "o" "u"]
containsMoreThan3Es?: function [w][
if 4 > size match w "e" -> return false
loop otherVowels 'v [
if contains? w v -> return false
]
return true
]
loop words 'word [
if containsMoreThan3Es? word ->
print word
... |
Write the same code in Arturo as shown below in C. | #include <stdio.h>
int min(int a, int b) {
if (a < b) return a;
return b;
}
int main() {
int n;
int numbers1[5] = {5, 45, 23, 21, 67};
int numbers2[5] = {43, 22, 78, 46, 38};
int numbers3[5] = {9, 98, 12, 98, 53};
int numbers[5] = {};
for (n = 0; n < 5; ++n) {
numbers[n] = min... | Red [
Red-version: 0.6.4
Description: "Find the element-wise minimum of three lists"
]
numbers1: [5 45 23 21 67]
numbers2: [43 22 78 46 38]
numbers3: [9 98 12 98 53]
length: length? numbers1
result: append/dup [] 0 length
repeat i length [
result/:i: min min numbers1/:i numbers2/:i numbers3/:i
]
print resu... |
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include <stdio.h>
void mosaicMatrix(unsigned int n) {
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if ((i + j) % 2 == 0) {
printf("%s ", "1");
} else {
printf("%s ", "0");
}
}
printf("\n");
}
}
in... | drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(equal? x%2 y%2)? -> 1 -> 0
]
]
drawSquare 6
print ""
drawSquare 9
|
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_SIZE 80
bool is_vowel(char ch) {
switch (ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
return true;
}
return false;
}
bo... | words: read.lines relative "unixdict.txt"
vowels: [`a` `e` `i` `o` `u`]
alternatingVC?: function [w][
lookForConsonant: true
if contains? vowels first w -> lookForConsonant: false
loop w 'c [
if xnor? lookForConsonant
contains? vowels c -> return false
lookForConsonant: no... |
Change the programming language of this snippet from C to Arturo without modifying what it does. | #include <stdio.h>
int divisible(int n) {
int p = 1;
int c, d;
for (c=n; c; c /= 10) {
d = c % 10;
if (!d || n % d) return 0;
p *= d;
}
return n % p;
}
int main() {
int n, c=0;
for (n=1; n<1000; n++) {
if (divisible(n)) {
printf("%... | valid?: function [n][
digs: digits n
facts: factors n
and? [not? in? product digs facts]
[every? digs 'd -> in? d facts]
]
print select 1..999 => valid?
|
Maintain the same structure and functionality when rewriting this code in Arturo. | #include <stdio.h>
void lshift(int *l, size_t n) {
int i, f;
if (n < 2) return;
f = l[0];
for (i = 0; i < n-1; ++i) l[i] = l[i+1];
l[n-1] = f;
}
int main() {
int l[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
size_t n = 9;
printf("Original list : ");
for (i = 0; i < n; ++i) pri... | lst: [1 2 3 4 5 6 7 8 9]
print rotate.left lst 3
|
Convert this C block to Arturo, preserving its control flow and logic. | #include <stdio.h>
#define LIMIT 100000
int digitset(int num, int base) {
int set;
for (set = 0; num; num /= base)
set |= 1 << num % base;
return set;
}
int main() {
int i, c = 0;
for (i = 0; i < LIMIT; i++)
if (digitset(i,10) == digitset(i,16))
printf("%6d%c", i, ++c%1... | valid?: function [n][
equal? sort unique digits n
sort unique digits.base:16 n
]
print select 0..100000 => valid?
|
Generate a Arturo translation of this C snippet without changing its computational steps. | #include <stdio.h>
void specialMatrix(unsigned int n) {
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == j || i + j == n - 1) {
printf("%d ", 1);
} else {
printf("%d ", 0);
}
}
printf("\n");
}
... | drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(any? @[x=y side=x+y-1])? -> 1 -> 0
]
]
drawSquare 6
print ""
drawSquare 9
|
Produce a language-to-language conversion: from C to Arturo, same semantics. | #include <stdio.h>
#include <string.h>
int main() {
char word[128];
FILE *f = fopen("unixdict.txt","r");
if (!f) {
fprintf(stderr, "Cannot open unixdict.txt\n");
return -1;
}
while (!feof(f)) {
fgets(word, sizeof(word), f);
if (strlen(word) > 12 && ... | Red[]
foreach word read/lines %unixdict.txt [
if all [11 < length? word find word "the"] [print word]
]
|
Translate this program into Arturo but keep the logic exactly as in C. | #include <stdio.h>
void hollowMatrix(unsigned int n) {
int i, j;
for (i = 0; i < n; ++i) {
for (j = 0; j < n; ++j) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
printf("%d ", 1);
} else {
printf("%d ", 0);
}
}
p... | drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(any? @[x=1 y=1 x=side y=side])? -> 1 -> 0
]
]
drawSquare 4
print ""
drawSquare 6
|
Ensure the translated Arturo code behaves exactly like the original C snippet. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node_t {
char *elem;
int length;
struct node_t *next;
} node;
node *make_node(char *s) {
node *t = malloc(sizeof(node));
t->elem = s;
t->length = strlen(s);
t->next = NULL;
return t;
}
void append_node(node *hea... | lcs: function [l][
ret: ""
idx: 0
lst: map l => reverse
while [true] [
thisLetter: ""
loop lst 'word [
if idx=size word -> return reverse ret
if thisLetter="" -> thisLetter: get split word idx
if thisLetter<>get split word idx -> return reverse ret
]
... |
Transform the following C implementation into Arturo, maintaining the same output and logic. | #include <locale.h>
#include <stdio.h>
int main()
{
unsigned long long int trillion = 1000000000000;
setlocale(LC_NUMERIC,"");
printf("Locale : %s, One Trillion : %'llu\n", setlocale(LC_CTYPE,NULL),trillion);
return 0;
}
| a: 1234567
b: 3.14
print a
print b
|
Port the provided C code into Arturo while preserving the original functionality. | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
int compare(const void *a, const void *b) {
int ia = *(int*)a;
int ib = *(int*)b;
return (ia>ib) - (ia<ib);
}
int main() {
int pows[16];
int a, b, i=0;
for (a=2; a<=5; a++)
for (b=2; b<=5; b++)
pows[i++] = pow(a,... | print sort unique flatten map 2..5 'a [
map 2..5 'b -> a^b
]
|
Maintain the same structure and functionality when rewriting this code in Arturo. | #include <stdio.h>
#include <string.h>
char *uniques(char *str[], char *buf) {
static unsigned counts[256];
unsigned i;
char *s, *o = buf;
memset(counts, 0, 256 * sizeof(unsigned));
for (; *str; str++)
for (s = *str; *s; s++)
counts[(unsigned) *s]++;
for (i=0; i<25... | arr: ["133252abcdeeffd" "a6789798st" "yxcdfgxcyz"]
str: join arr
print sort select split str 'ch -> 1 = size match str ch
|
Rewrite the snippet below in Arturo so it works the same as the original C code. | #include <stdio.h>
#include <gmp.h>
void jacobsthal(mpz_t r, unsigned long n) {
mpz_t s;
mpz_init(s);
mpz_set_ui(r, 1);
mpz_mul_2exp(r, r, n);
mpz_set_ui(s, 1);
if (n % 2) mpz_neg(s, s);
mpz_sub(r, r, s);
mpz_div_ui(r, r, 3);
}
void jacobsthal_lucas(mpz_t r, unsigned long n) {
mpz_... | J: function [n]-> ((2^n) - (neg 1)^n)/3
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
printFirst: function [label, what, predicate, count][
print ["First" count label++":"]
result: new []
i: 0
while [count > size result][
num: do ~"|what| i"
if do predicate ->... |
Convert this C block to Arturo, preserving its control flow and logic. | #include <stdio.h>
#include <gmp.h>
void jacobsthal(mpz_t r, unsigned long n) {
mpz_t s;
mpz_init(s);
mpz_set_ui(r, 1);
mpz_mul_2exp(r, r, n);
mpz_set_ui(s, 1);
if (n % 2) mpz_neg(s, s);
mpz_sub(r, r, s);
mpz_div_ui(r, r, 3);
}
void jacobsthal_lucas(mpz_t r, unsigned long n) {
mpz_... | J: function [n]-> ((2^n) - (neg 1)^n)/3
JL: function [n]-> (2^n) + (neg 1)^n
JO: function [n]-> (J n) * (J n+1)
printFirst: function [label, what, predicate, count][
print ["First" count label++":"]
result: new []
i: 0
while [count > size result][
num: do ~"|what| i"
if do predicate ->... |
Transform the following C implementation into Arturo, maintaining the same output and logic. | #include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#define PRIME_COUNT 100000
int64_t PRIMES[PRIME_COUNT];
size_t primeSize = 0;
bool isPrime(int n) {
size_t i = 0;
for (i = 0; i < primeSize; i++) {
int64_t p = PRIMES[i];
if (n == p) {
return true;
}
if (n... | LIM: 1000000
findDiffs: function [r][
if r=[1] -> return [[2 3]]
i: 3
tupled: map 0..dec size r 'x -> fold slice r 0 x [a b][a+b]
diffs: new []
while [i < LIM][
if prime? i [
prset: map tupled 't -> i + t
if every? prset 'elem -> prime? elem [
'diffs ... |
Keep all operations the same but rewrite the snippet in Arturo. | #include <stdio.h>
#include <stdint.h>
uint8_t prime(uint8_t n) {
uint8_t f;
if (n < 2) return 0;
for (f = 2; f < n; f++) {
if (n % f == 0) return 0;
}
return 1;
}
uint8_t digit_sum(uint8_t n, uint8_t base) {
uint8_t s = 0;
do {s += n % base;} while (n /= base);
return s;
}
... | loop split.every: 10
select 1..199 'n [ and? prime? sum digits.base: 2 n
prime? sum digits.base: 3 n ] 'a ->
print map a => [pad to :string & 4]
|
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include<stdio.h>
#include<stdlib.h>
int isprime( int p ) {
int i;
if(p==2) return 1;
if(!(p%2)) return 0;
for(i=3; i*i<=p; i+=2) {
if(!(p%i)) return 0;
}
return 1;
}
int prime( int n ) {
int p, pn=1;
if(n==1) return 2;
for(p=3;pn<n;p+=2) {
if(isprime(p)) pn++;
}... | primes: select 2..110000 => prime?
print primes\[10000]
|
Port the following code from C to Arturo with equivalent syntax and logic. | #include<stdio.h>
#include<stdlib.h>
int isprime( int p ) {
int i;
if(p==2) return 1;
if(!(p%2)) return 0;
for(i=3; i*i<=p; i+=2) {
if(!(p%i)) return 0;
}
return 1;
}
int prime( int n ) {
int p, pn=1;
if(n==1) return 2;
for(p=3;pn<n;p+=2) {
if(isprime(p)) pn++;
}... | primes: select 2..110000 => prime?
print primes\[10000]
|
Translate this program into Arturo but keep the logic exactly as in C. | #include <stdbool.h>
#include <stdio.h>
bool is_prime(int n) {
int i = 5;
if (n < 2) {
return false;
}
if (n % 2 == 0) {
return n == 2;
}
if (n % 3 == 0) {
return n == 3;
}
while (i * i <= n) {
if (n % i == 0) {
return false;
}
... | print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
print "------------------------------"
s: 0
idx: 0
loop 2..999 'n [
if prime? n [
idx: idx + 1
s: s + n
if prime? s ->
print (pad to :string idx 6) ++ " | " ++
(pad t... |
Write the same algorithm in Arturo as shown in this C implementation. | #include <stdbool.h>
#include <stdio.h>
bool is_prime(int n) {
int i = 5;
if (n < 2) {
return false;
}
if (n % 2 == 0) {
return n == 2;
}
if (n % 3 == 0) {
return n == 3;
}
while (i * i <= n) {
if (n % i == 0) {
return false;
}
... | print (pad "index" 6) ++ " | " ++
(pad "prime" 6) ++ " | " ++
(pad "prime sum" 11)
print "------------------------------"
s: 0
idx: 0
loop 2..999 'n [
if prime? n [
idx: idx + 1
s: s + n
if prime? s ->
print (pad to :string idx 6) ++ " | " ++
(pad t... |
Change the following C code into Arturo without altering its purpose. | #include <sys/ioctl.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int
main()
{
struct winsize ws;
int fd;
fd = open("/dev/tty", O_RDWR);
if (fd < 0)
err(1, "/dev/tty");
if (ioctl(fd, TIOCGWINSZ, &ws) < 0)
err(1, "/dev/tty");
printf("%d rows by %d columns\n", ws.ws... | print ["Terminal width:" terminal\width]
print ["Terminal height:" terminal\height]
|
Rewrite the snippet below in Arturo so it works the same as the original C code. | #include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
int d;
if (n < 2) return false;
if (!(n%2)) return n == 2;
if (!(n%3)) return n == 3;
d = 5;
while (d*d <= n) {
if (!(n%d)) return false;
d += 2;
if (!(n%d)) return false;
d += 4;
}
return tru... | specials: new [2 3]
lim: 1050
lastP: 3
lastGap: 1
loop 5.. .step:2 lim 'n [
if not? prime? n -> continue
if lastGap < n - lastP [
lastGap: n - lastP
lastP: n
'specials ++ n
]
]
print "List of next special primes less than 1050:"
print specials
|
Can you help me rewrite this code in Arturo instead of C, keeping it the same logically? | #include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
int d;
if (n < 2) return false;
if (!(n%2)) return n == 2;
if (!(n%3)) return n == 3;
d = 5;
while (d*d <= n) {
if (!(n%d)) return false;
d += 2;
if (!(n%d)) return false;
d += 4;
}
return tru... | specials: new [2 3]
lim: 1050
lastP: 3
lastGap: 1
loop 5.. .step:2 lim 'n [
if not? prime? n -> continue
if lastGap < n - lastP [
lastGap: n - lastP
lastP: n
'specials ++ n
]
]
print "List of next special primes less than 1050:"
print specials
|
Port the following code from C to Arturo with equivalent syntax and logic. | #include<stdio.h>
int main()
{
int num = 9876432,diff[] = {4,2,2,2},i,j,k=0;
char str[10];
start:snprintf(str,10,"%d",num);
for(i=0;str[i+1]!=00;i++){
if(str[i]=='0'||str[i]=='5'||num%(str[i]-'0')!=0){
num -= diff[k];
k = (k+1)%4;
goto start;
}
for(j=i+1;str[j]!=00;j++)
if(str[i]==str... | lynchBell?: function [num][
hset: new []
loop digits num 'd [
if d=0 -> return false
if or? [0 <> mod num d]
[contains? hset d] -> return false
'hset ++ d
unique 'hset
]
return true
]
Magic: 9 * 8 * 7
High: ((from.hex "9876432") / Magic) * Magic
loop rang... |
Can you help me rewrite this code in Arturo instead of C, keeping it the same logically? | #include<stdio.h>
int main()
{
int num = 9876432,diff[] = {4,2,2,2},i,j,k=0;
char str[10];
start:snprintf(str,10,"%d",num);
for(i=0;str[i+1]!=00;i++){
if(str[i]=='0'||str[i]=='5'||num%(str[i]-'0')!=0){
num -= diff[k];
k = (k+1)%4;
goto start;
}
for(j=i+1;str[j]!=00;j++)
if(str[i]==str... | lynchBell?: function [num][
hset: new []
loop digits num 'd [
if d=0 -> return false
if or? [0 <> mod num d]
[contains? hset d] -> return false
'hset ++ d
unique 'hset
]
return true
]
Magic: 9 * 8 * 7
High: ((from.hex "9876432") / Magic) * Magic
loop rang... |
Transform the following C implementation into Arturo, maintaining the same output and logic. | #include <stdlib.h>
#include <stdio.h>
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
int jacobi(unsigned long a, unsigned long n) {
if (a >= n) a %= n;
int result = 1;
while (a) {
while ((a & 1) == 0) {
a >>= 1;
if ((n & 7) == 3 || (n & 7) == 5) result = -result;
}
SWAP(a, n);
if ((a ... | jacobi: function [n,k][
N: n % k
K: k
result: 1
while [N <> 0][
while [even? N][
N: shr N 1
if contains? [3 5] and K 7 ->
result: neg result
]
[N,K]: @[K,N]
if and? 3=and N 3 3=and K 3 ->
result: neg result
N: N... |
Write a version of this C function in Arturo with identical behavior. | #include <stdlib.h>
#include <stdio.h>
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
int jacobi(unsigned long a, unsigned long n) {
if (a >= n) a %= n;
int result = 1;
while (a) {
while ((a & 1) == 0) {
a >>= 1;
if ((n & 7) == 3 || (n & 7) == 5) result = -result;
}
SWAP(a, n);
if ((a ... | jacobi: function [n,k][
N: n % k
K: k
result: 1
while [N <> 0][
while [even? N][
N: shr N 1
if contains? [3 5] and K 7 ->
result: neg result
]
[N,K]: @[K,N]
if and? 3=and N 3 3=and K 3 ->
result: neg result
N: N... |
Preserve the algorithm and functionality while converting the code from C to Arturo. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
double det_in(double **in, int n, int perm)
{
if (n == 1) return in[0][0];
double sum = 0, *m[--n];
for (int i = 0; i < n; i++)
m[i] = in[i + 1] + 1;
for (int i = 0, sgn = 1; i <= n; i++) {
sum += sgn * (in[i][0] * det_in(m, n, perm));
if (i == n) ... | printMatrix: function [m][
loop m 'row -> print map row 'val [pad to :string .format:".2f" val 6]
print "--------------------------------"
]
permutations: function [arr][
d: 1
c: array.of: size arr 0
xs: new arr
sign: 1
ret: new @[@[xs, sign]]
while [true][
while [d > 1][
... |
Change the programming language of this snippet from C to Arturo without modifying what it does. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
double det_in(double **in, int n, int perm)
{
if (n == 1) return in[0][0];
double sum = 0, *m[--n];
for (int i = 0; i < n; i++)
m[i] = in[i + 1] + 1;
for (int i = 0, sgn = 1; i <= n; i++) {
sum += sgn * (in[i][0] * det_in(m, n, perm));
if (i == n) ... | printMatrix: function [m][
loop m 'row -> print map row 'val [pad to :string .format:".2f" val 6]
print "--------------------------------"
]
permutations: function [arr][
d: 1
c: array.of: size arr 0
xs: new arr
sign: 1
ret: new @[@[xs, sign]]
while [true][
while [d > 1][
... |
Translate this program into Arturo but keep the logic exactly as in C. | #include <stdio.h>
unsigned modpow(unsigned b, unsigned e, unsigned m)
{
unsigned p;
for (p = 1; e; e >>= 1) {
if (e & 1)
p = p * b % m;
b = b * b % m;
}
return p;
}
int is_deceptive(unsigned n)
{
unsigned x;
if (n & 1 && n % 3 && n % 5) {
for (x = 7; x * x ... | deceptive?: function [n][
and? -> not? prime? n
-> zero? (to :integer repeat "1" n-1) % n
]
cnt: 0
i: 3
while [cnt < 10][
if deceptive? i [
print i
cnt: cnt + 1
]
i: i + 2
]
|
Convert this C block to Arturo, preserving its control flow and logic. | #include <stdio.h>
unsigned modpow(unsigned b, unsigned e, unsigned m)
{
unsigned p;
for (p = 1; e; e >>= 1) {
if (e & 1)
p = p * b % m;
b = b * b % m;
}
return p;
}
int is_deceptive(unsigned n)
{
unsigned x;
if (n & 1 && n % 3 && n % 5) {
for (x = 7; x * x ... | deceptive?: function [n][
and? -> not? prime? n
-> zero? (to :integer repeat "1" n-1) % n
]
cnt: 0
i: 3
while [cnt < 10][
if deceptive? i [
print i
cnt: cnt + 1
]
i: i + 2
]
|
Change the following C code into Arturo without altering its purpose. | #include <stdio.h>
#include <string.h>
int digitSum(int n) {
int s = 0;
do {s += n % 10;} while (n /= 10);
return s;
}
int digitSumIsSubstring(int n) {
char s_n[32], s_ds[32];
sprintf(s_n, "%d", n);
sprintf(s_ds, "%d", digitSum(n));
return strstr(s_n, s_ds) != NULL;
}
int main() {
int... | print select 1..999 'num ->
contains? to :string num
to :string sum digits num
|
Convert the following code from C to Arturo, ensuring the logic remains intact. | #include <stdio.h>
#include <string.h>
int digitSum(int n) {
int s = 0;
do {s += n % 10;} while (n /= 10);
return s;
}
int digitSumIsSubstring(int n) {
char s_n[32], s_ds[32];
sprintf(s_n, "%d", n);
sprintf(s_ds, "%d", digitSum(n));
return strstr(s_n, s_ds) != NULL;
}
int main() {
int... | print select 1..999 'num ->
contains? to :string num
to :string sum digits num
|
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include<stdlib.h>
#include<stdio.h>
#include<time.h>
void sattoloCycle(void** arr,int count){
int i,j;
void* temp;
if(count<2)
return;
for(i=count-1;i>=1;i--){
j = rand()%i;
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
int main(int argC,char* argV[])
{
int i;
if(argC==1)
printf("Usage : ... | cycle: function [arr][
if 2 > size arr -> return arr
lastIndex: (size arr)-1
result: new arr
loop lastIndex..1 'i [
j: random 0 i-1
tmp: result\[i]
set result i result\[j]
set result j tmp
]
return result
]
lists: [
[]
[10]
[10 20]
[10 20 30]
... |
Change the following C code into Arturo without altering its purpose. | #include<stdlib.h>
#include<stdio.h>
#include<time.h>
void sattoloCycle(void** arr,int count){
int i,j;
void* temp;
if(count<2)
return;
for(i=count-1;i>=1;i--){
j = rand()%i;
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
int main(int argC,char* argV[])
{
int i;
if(argC==1)
printf("Usage : ... | cycle: function [arr][
if 2 > size arr -> return arr
lastIndex: (size arr)-1
result: new arr
loop lastIndex..1 'i [
j: random 0 i-1
tmp: result\[i]
set result i result\[j]
set result j tmp
]
return result
]
lists: [
[]
[10]
[10 20]
[10 20 30]
... |
Port the following code from C to Arturo with equivalent syntax and logic. | #include <ftplib.h>
int main(void)
{
netbuf *nbuf;
FtpInit();
FtpConnect("kernel.org", &nbuf);
FtpLogin("anonymous", "", nbuf);
FtpOptions(FTPLIB_CONNMODE, FTPLIB_PASSIVE, nbuf);
FtpChdir("pub/linux/kernel", nbuf);
FtpDir((void*)0, ".", nbuf);
FtpGet("ftp.README", "README", FTPLIB_ASCI... | system/schemes/ftp/passive: on
print read ftp://kernel.org/pub/linux/kernel/
write/binary %README read/binary ftp://kernel.org/pub/linux/kernel/README
|
Maintain the same structure and functionality when rewriting this code in Arturo. | #include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
const char *code =
"CREATE TABLE address (\n"
" addrID INTEGER PRIMARY KEY AUTOINCREMENT,\n"
" addrStreet TEXT NOT NULL,\n"
" addrCity TEXT NOT NULL,\n"
" addrState TEXT NOT NULL,\n"
" addrZIP TEXT NOT NULL)\n" ;
int main()
{
sqlite3 *db = NULL;
... | db: open.sqlite "addresses.db"
query db {!sql
CREATE TABLE address (
addrID INTEGER PRIMARY KEY AUTOINCREMENT,
addrStreet TEXT NOT NULL,
addrCity TEXT NOT NULL,
addrState TEXT NOT NULL,
addrZIP TEXT NOT NULL
)
}
close db
|
Convert this C snippet to Arturo and keep its semantics consistent. | #include <stdio.h>
#include <stdlib.h>
int cycleSort(int * list, size_t l_len);
void show_array(int * array, size_t a_len);
int cycleSort(int * list, size_t l_len)
{
int writes = 0;
for (int cycleStart = 0; cycleStart < l_len - 1; ++cycleStart)
{
int item = list[cycleStart];
int swap_tmp;
... | cycleSort: function [items][
a: new items
position: 0
loop 0..dec dec size a 'cycleStart [
item: a\[cycleStart]
position: cycleStart
loop (cycleStart+1)..dec size a 'i [
if (get a i) < item -> position: position + 1
]
if position = cycleStart -> continue
... |
Change the programming language of this snippet from C to Arturo without modifying what it does. | #include <stdio.h>
#include <stdlib.h>
int cycleSort(int * list, size_t l_len);
void show_array(int * array, size_t a_len);
int cycleSort(int * list, size_t l_len)
{
int writes = 0;
for (int cycleStart = 0; cycleStart < l_len - 1; ++cycleStart)
{
int item = list[cycleStart];
int swap_tmp;
... | cycleSort: function [items][
a: new items
position: 0
loop 0..dec dec size a 'cycleStart [
item: a\[cycleStart]
position: cycleStart
loop (cycleStart+1)..dec size a 'i [
if (get a i) < item -> position: position + 1
]
if position = cycleStart -> continue
... |
Write a version of this C function in Arturo with identical behavior. | #include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
bool isPrime(int64_t n) {
int64_t i;
if (n < 2) return false;
if (n % 2 == 0) return n == 2;
if (n % 3 == 0) return n == 3;
if (n % 5 == 0) return n == 5;
if (n % 7 == 0) return n == 7;
if (n % 11 == 0) return n == 11;
... | pairsOfPrimes: function [upperLim][
count: 0
j: 0
k: 1
i: 0
while [i=<upperLim][
i: (6 * k) - 1
j: i + 2
if and? [prime? i] [prime? j] [
count: count + 1
]
k: k + 1
]
return count + 1
]
ToNum: 10
while [ToNum =< 1000000][
x: pairsOfPri... |
Change the following C code into Arturo without altering its purpose. | #include <stdio.h>
typedef char bool;
#define TRUE 1
#define FALSE 0
bool same_digits(int n, int b) {
int f = n % b;
n /= b;
while (n > 0) {
if (n % b != f) return FALSE;
n /= b;
}
return TRUE;
}
bool is_brazilian(int n) {
int b;
if (n < 7) return FALSE;
if (!(n % 2) ... | brazilian?: function [n][
if n < 7 -> return false
if zero? and n 1 -> return true
loop 2..n-2 'b [
if 1 = size unique digits.base:b n ->
return true
]
return false
]
printFirstByRule: function [rule,title][
print ~"First 20 |title|brazilian numbers:"
i: 7
found: new... |
Produce a functionally identical Arturo code for the snippet given in C. | #include <stdio.h>
typedef char bool;
#define TRUE 1
#define FALSE 0
bool same_digits(int n, int b) {
int f = n % b;
n /= b;
while (n > 0) {
if (n % b != f) return FALSE;
n /= b;
}
return TRUE;
}
bool is_brazilian(int n) {
int b;
if (n < 7) return FALSE;
if (!(n % 2) ... | brazilian?: function [n][
if n < 7 -> return false
if zero? and n 1 -> return true
loop 2..n-2 'b [
if 1 = size unique digits.base:b n ->
return true
]
return false
]
printFirstByRule: function [rule,title][
print ~"First 20 |title|brazilian numbers:"
i: 7
found: new... |
Translate the given C code snippet into Arturo without altering its behavior. | #include<stdio.h>
int main()
{
FILE* fp = fopen("TAPE.FILE","w");
fprintf(fp,"This code should be able to write a file to magnetic tape.\n");
fprintf(fp,"The Wikipedia page on Magnetic tape data storage shows that magnetic tapes are still in use.\n");
fprintf(fp,"In fact, the latest format, at the time of writin... | write "TAPE.FILE" {
This code
should be able to write
a file
to magnetic tape
}
|
Write a version of this C function in Arturo with identical behavior. | #include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
typedef int bool;
int main() {
int i, n, k = 0, next, *a;
bool foundDup = FALSE;
gboolean alreadyUsed;
GHashTable* used = g_hash_table_new(g_direct_hash, g_direct_equal);
GHashTable* used1000 = g_hash_table_new(g_direct_hash, g_direct_equ... | recamanSucc: function [seen, n, r].memoize[
back: r - n
(or? 0 > back contains? seen back)? -> n + r
-> back
]
recamanUntil: function [p][
n: new 1
r: 0
rs: new @[r]
seen: rs
blnNew: true
while [not? do p][
r: recamanSucc seen n r
... |
Generate an equivalent Arturo version of this C code. | #include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>
typedef int bool;
int main() {
int i, n, k = 0, next, *a;
bool foundDup = FALSE;
gboolean alreadyUsed;
GHashTable* used = g_hash_table_new(g_direct_hash, g_direct_equal);
GHashTable* used1000 = g_hash_table_new(g_direct_hash, g_direct_equ... | recamanSucc: function [seen, n, r].memoize[
back: r - n
(or? 0 > back contains? seen back)? -> n + r
-> back
]
recamanUntil: function [p][
n: new 1
r: 0
rs: new @[r]
seen: rs
blnNew: true
while [not? do p][
r: recamanSucc seen n r
... |
Write a version of this C function in Arturo with identical behavior. | #include <stdio.h>
#include <stdlib.h>
typedef struct func_t *func;
typedef struct func_t {
func (*fn) (func, func);
func _;
int num;
} func_t;
func new(func(*f)(func, func), func _) {
func x = malloc(sizeof(func_t));
x->fn = f;
x->_ = _;
x->num = 0;
... | Y: closure [g] [do func [f] [f :f] closure [f] [g func [x] [do f :f :x]]]
|
Write the same algorithm in Arturo as shown in this C implementation. | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
typedef double Fp;
typedef struct { Fp x, y, r; } Circle;
Circle circles[] = {
{ 1.6417233788, 1.6121789534, 0.0848270516},
{-1.4944608174, 1.2077959613, 1.1039549836},
{ 0.6110294452, -0.6907087527, 0.908916... | circles: @[
@[ 1.6417233788 1.6121789534 0.0848270516]
@[neg 1.4944608174 1.2077959613 1.1039549836]
@[ 0.6110294452 neg 0.6907087527 0.9089162485]
@[ 0.3844862411 0.2923344616 0.2375743054]
@[neg 0.2495892950 neg 0.3832854473 1.0845181219]
@[ 1.7813504266 1.617823703... |
Produce a language-to-language conversion: from C to Arturo, same semantics. | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
typedef double Fp;
typedef struct { Fp x, y, r; } Circle;
Circle circles[] = {
{ 1.6417233788, 1.6121789534, 0.0848270516},
{-1.4944608174, 1.2077959613, 1.1039549836},
{ 0.6110294452, -0.6907087527, 0.908916... | circles: @[
@[ 1.6417233788 1.6121789534 0.0848270516]
@[neg 1.4944608174 1.2077959613 1.1039549836]
@[ 0.6110294452 neg 0.6907087527 0.9089162485]
@[ 0.3844862411 0.2923344616 0.2375743054]
@[neg 0.2495892950 neg 0.3832854473 1.0845181219]
@[ 1.7813504266 1.617823703... |
Ensure the translated Arturo code behaves exactly like the original C snippet. | #include <stdio.h>
int main() {
int n, b, d;
unsigned long long i, j, sum, fact[12];
fact[0] = 1;
for (n = 1; n < 12; ++n) {
fact[n] = fact[n-1] * n;
}
for (b = 9; b <= 12; ++b) {
printf("The factorions for base %d are:\n", b);
for (i = 1; i < 1500000; ++i) {
... | factorials: [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800]
factorion?: function [n, base][
try? [
n = sum map digits.base:base n 'x -> factorials\[x]
]
else [
print ["n:" n "base:" base]
false
]
]
loop 9..12 'base ->
print ["Base" base "factorions:" select 1..45000... |
Keep all operations the same but rewrite the snippet in Arturo. | #include <stdio.h>
int main() {
int n, b, d;
unsigned long long i, j, sum, fact[12];
fact[0] = 1;
for (n = 1; n < 12; ++n) {
fact[n] = fact[n-1] * n;
}
for (b = 9; b <= 12; ++b) {
printf("The factorions for base %d are:\n", b);
for (i = 1; i < 1500000; ++i) {
... | factorials: [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800]
factorion?: function [n, base][
try? [
n = sum map digits.base:base n 'x -> factorials\[x]
]
else [
print ["n:" n "base:" base]
false
]
]
loop 9..12 'base ->
print ["Base" base "factorions:" select 1..45000... |
Write the same code in Arturo as shown below in C. | #include <stdio.h>
unsigned int divisor_sum(unsigned int n) {
unsigned int total = 1, power = 2;
unsigned int p;
for (; (n & 1) == 0; power <<= 1, n >>= 1) {
total += power;
}
for (p = 3; p * p <= n; p += 2) {
unsigned int sum = 1;
for (power = p; n % p == 0; powe... | loop split.every:10 map 1..100 'x -> sum factors x 'row [
print map row 'r -> pad to :string r 4
]
|
Port the following code from C to Arturo with equivalent syntax and logic. | #include <stdio.h>
unsigned int divisor_sum(unsigned int n) {
unsigned int total = 1, power = 2;
unsigned int p;
for (; (n & 1) == 0; power <<= 1, n >>= 1) {
total += power;
}
for (p = 3; p * p <= n; p += 2) {
unsigned int sum = 1;
for (power = p; n % p == 0; powe... | loop split.every:10 map 1..100 'x -> sum factors x 'row [
print map row 'r -> pad to :string r 4
]
|
Produce a language-to-language conversion: from C to Arturo, same semantics. | #include <stdio.h>
#include <stdlib.h>
int isprime( int n ) {
int i;
if (n<2) return 0;
for(i=2; i*i<=n; i++) {
if (n % i == 0) {return 0;}
}
return 1;
}
int main(void) {
int n = 0, p = 1;
while (n<22) {
printf( "%d ", n );
p++;
if (isprime(p)) n+=1;
}
return 0;
}
| primes: select 2..1000 => prime?
piprimes: function [n] -> size select primes 'z [z =< n]
loop split.every: 10 select map 1..100 => piprimes => [& < 22] 'a ->
print map a => [pad to :string & 3]
|
Change the programming language of this snippet from C to Arturo without modifying what it does. | #include <stdio.h>
#include <stdlib.h>
int isprime( int n ) {
int i;
if (n<2) return 0;
for(i=2; i*i<=n; i++) {
if (n % i == 0) {return 0;}
}
return 1;
}
int main(void) {
int n = 0, p = 1;
while (n<22) {
printf( "%d ", n );
p++;
if (isprime(p)) n+=1;
}
return 0;
}
| primes: select 2..1000 => prime?
piprimes: function [n] -> size select primes 'z [z =< n]
loop split.every: 10 select map 1..100 => piprimes => [& < 22] 'a ->
print map a => [pad to :string & 3]
|
Keep all operations the same but rewrite the snippet in Arturo. | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
int interactiveCompare(const void *x1, const void *x2)
{
const char *s1 = *(const char * const *)x1;
const char *s2 = *(const char * const *)x2;
static int count = 0;
printf("(%d) Is %s <, ==, or > %s? Answer -1, 0, or 1: ", ++count, s1, s2);
int res... | lst: ["violet" "red" "green" "indigo" "blue" "yellow" "orange"]
count: 0
findSpot: function [l,e][
if empty? l -> return 0
loop.with:'i l 'item [
answer: input ~"Is |item| greater than |e| [y/n]? "
if answer="y" -> return i
]
return dec size l
]
sortedLst: new []
loop lst 'element -... |
Convert the following code from C to Arturo, ensuring the logic remains intact. | #include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
void mpz_factors(mpz_t n) {
int factors = 0;
mpz_t s, m, p;
mpz_init(s), mpz_init(m), mpz_init(p);
mpz_set_ui(m, 3);
mpz_set(p, n);
mpz_sqrt(s, p);
while (mpz_cmp(m, s) < 0) {
if (mpz_divisible_p(p, m)) {
gmp_printf("%Zd ", m);
mpz... | nPowers: [1 2 4 8 16 32 64 128 256 512]
fermatSet: map 0..9 'x -> 1 + 2 ^ nPowers\[x]
loop 0..9 'i ->
print ["F(" i ") =" fermatSet\[i]]
print ""
loop 0..9 'i ->
print ["Prime factors of F(" i ") =" factors.prime fermatSet\[i]]
|
Write a version of this C function in Arturo with identical behavior. | #include <stdio.h>
#include <stdlib.h>
void bead_sort(int *a, int len)
{
int i, j, max, sum;
unsigned char *beads;
# define BEAD(i, j) beads[i * max + j]
for (i = 1, max = a[0]; i < len; i++)
if (a[i] > max) max = a[i];
beads = calloc(1, max * len);
for (i = 0; i < len; i++)
for (j = 0; j < a[i]; j++)
... | beadSort: function [items][
a: new items
m: neg infinity
s: 0
loop a 'x [
if x > m -> m: x
]
beads: array.of: m * size a 0
loop 0..dec size a 'i [
loop 0..dec a\[i] 'j ->
beads\[j + i * m]: 1
]
loop 0..dec m 'j [
s: 0
loop 0..dec size a... |
Please provide an equivalent version of this C code in Arturo. | #include <stdio.h>
#include <math.h>
int main() {
const int N = 2;
int base = 10;
int c1 = 0;
int c2 = 0;
int k;
for (k = 1; k < pow(base, N); k++) {
c1++;
if (k % (base - 1) == (k * k) % (base - 1)) {
c2++;
printf("%d ", k);
}
}
printf(... | N: 2
base: 10
c1: 0
c2: 0
loop 1..(base^N)-1 'k [
c1: c1 + 1
if (k%base-1)= (k*k)%base-1 [
c2: c2 + 1
prints ~"|k| "
]
]
print ""
print ["Trying" c2 "numbers instead of" c1 "numbers saves" 100.0 - 100.0*c2//c1 "%"]
|
Change the following C code into Arturo without altering its purpose. | #include <stdio.h>
#include <math.h>
int main() {
const int N = 2;
int base = 10;
int c1 = 0;
int c2 = 0;
int k;
for (k = 1; k < pow(base, N); k++) {
c1++;
if (k % (base - 1) == (k * k) % (base - 1)) {
c2++;
printf("%d ", k);
}
}
printf(... | N: 2
base: 10
c1: 0
c2: 0
loop 1..(base^N)-1 'k [
c1: c1 + 1
if (k%base-1)= (k*k)%base-1 [
c2: c2 + 1
prints ~"|k| "
]
]
print ""
print ["Trying" c2 "numbers instead of" c1 "numbers saves" 100.0 - 100.0*c2//c1 "%"]
|
Convert this C snippet to Arturo and keep its semantics consistent. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XOPEN_SOURCE
#define __USE_XOPEN
#include <time.h>
#define DB "database.csv"
#define TRY(a) if (!(a)) {perror(#a);exit(1);}
#define TRY2(a) if((a)<0) {perror(#a);exit(1);}
#define FREE(a) if(a) {free(a);a=NULL;}
#define sort_by(foo) \
static int b... | rebol [author: "Nick Antonaccio"]
write/append %rdb "" db: load %rdb
switch system/options/args/1 [
"new" [write/append %rdb rejoin [now " " mold/only next system/options/args newline]]
"latest" [print copy/part tail sort/skip db 4 -4]
"latestcat" [
foreach cat unique extract at db 3 4 [
... |
Write the same code in Arturo as shown below in C. | void runCode(const char *code)
{
int c_len = strlen(code);
int i, bottles;
unsigned accumulator=0;
for(i=0;i<c_len;i++)
{
switch(code[i])
{
case 'Q':
printf("%s\n", code);
break;
case 'H':
printf("Hello, world!\... | hq9: function [source][
acc: 0
loop split source 'ch [
case [(lower ch)=]
when? ["h"]-> print "Hello, world!"
when? ["q"]-> print source
when? ["9"]-> print "99 bottles here ..."
when? ["+"]-> acc: acc+1
else []
]
return acc
]
a... |
Rewrite the snippet below in Arturo so it works the same as the original C code. | void runCode(const char *code)
{
int c_len = strlen(code);
int i, bottles;
unsigned accumulator=0;
for(i=0;i<c_len;i++)
{
switch(code[i])
{
case 'Q':
printf("%s\n", code);
break;
case 'H':
printf("Hello, world!\... | hq9: function [source][
acc: 0
loop split source 'ch [
case [(lower ch)=]
when? ["h"]-> print "Hello, world!"
when? ["q"]-> print source
when? ["9"]-> print "99 bottles here ..."
when? ["+"]-> acc: acc+1
else []
]
return acc
]
a... |
Convert this C snippet to Arturo and keep its semantics consistent. | #!/usr/local/bin/script_gcc.sh
#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
typedef char *STRING;
typedef enum{FALSE=0, TRUE=1} BOOL;
const STRING ENDCAT = NULL;
#define DIALECT "c"
const STRING
... | #!/usr/bin/env arturo
print "Hello from Arturo!"
|
Generate an equivalent Arturo version of this C code. | #!/usr/local/bin/script_gcc.sh
#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
typedef char *STRING;
typedef enum{FALSE=0, TRUE=1} BOOL;
const STRING ENDCAT = NULL;
#define DIALECT "c"
const STRING
... | #!/usr/bin/env arturo
print "Hello from Arturo!"
|
Convert the following code from C to Arturo, ensuring the logic remains intact. | #include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
int main()
{
Display *d;
XEvent event;
d = XOpenDisplay(NULL);
if ( d != NULL ) {
XGrabKey(d, XKeysymToKeycode(d, XStringToKeysym("F7")),
Mod1Mask,
DefaultRootWindow(d), True, GrabModeAsy... | REBOL [
Title: "Keyboard Macros"
URL: http://rosettacode.org/wiki/Keyboard_macros
]
view layout [
style btn button coal 46
across
display: h1 100 red maroon right "" return
btn "1" #"1" [set-face display "1"]
btn "+" #"+" [set-face display ""]
return
pad 54
btn "=" #"=" [set-face display ... |
Write the same algorithm in Arturo as shown in this C implementation. | #include <stdio.h>
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1;
for (; (n & 1) == 0; n >>= 1) {
++total;
}
for (unsigned int p = 3; p * p <= n; p += 2) {
unsigned int count = 1;
for (; n % p == 0; n /= p) {
++count;
}
... | tau: function [x] -> size factors x
loop split.every:20 1..100 => [
print map & => [pad to :string tau & 3]
]
|
Translate the given C code snippet into Arturo without altering its behavior. | #include <stdio.h>
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1;
for (; (n & 1) == 0; n >>= 1) {
++total;
}
for (unsigned int p = 3; p * p <= n; p += 2) {
unsigned int count = 1;
for (; n % p == 0; n /= p) {
++count;
}
... | tau: function [x] -> size factors x
loop split.every:20 1..100 => [
print map & => [pad to :string tau & 3]
]
|
Maintain the same structure and functionality when rewriting this code in Arturo. | #include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
const int MU_MAX = 1000000;
int i, j;
int *mu;
int sqroot;
sqroot = (int)sqrt(MU_MAX);
mu = malloc((MU_MAX + 1) * sizeof(int));
for (i = 0; i < MU_MAX;i++) {
mu[i] = 1;
}
for (i = 2... | mobius: function [n][
if n=0 -> return ""
if n=1 -> return 1
f: factors.prime n
if f <> unique f -> return 0
if? odd? size f -> return neg 1
else -> return 1
]
loop split.every:20 map 0..199 => mobius 'a ->
print map a => [pad to :string & 3]
|
Please provide an equivalent version of this C code in Arturo. | #include <stdio.h>
int main(){
int ptr=0, i=0, cell[7];
for( i=0; i<7; ++i) cell[i]=0;
ptr+= 1;
if(ptr>=7) perror("Program pointer overflow");
cell[ptr]+= 8;
while(cell[ptr])
{
ptr-= 1;
if(ptr<0) perror("Program pointer underflow");
cell[ptr]+= 9;
... |
Tape: [0]
DataPointer: new 0
InstructionPointer: new 0
precomputeJumps: function [][
vstack: new []
jumphash: new #[]
instrPointer: 0
while [instrPointer<CodeLength] [
command: get split Code instrPointer
if? command="[" -> 'vstack ++ instrPointer
else [
... |
Preserve the algorithm and functionality while converting the code from C to Arturo. | #include <stdio.h>
int main(){
int ptr=0, i=0, cell[7];
for( i=0; i<7; ++i) cell[i]=0;
ptr+= 1;
if(ptr>=7) perror("Program pointer overflow");
cell[ptr]+= 8;
while(cell[ptr])
{
ptr-= 1;
if(ptr<0) perror("Program pointer underflow");
cell[ptr]+= 9;
... |
Tape: [0]
DataPointer: new 0
InstructionPointer: new 0
precomputeJumps: function [][
vstack: new []
jumphash: new #[]
instrPointer: 0
while [instrPointer<CodeLength] [
command: get split Code instrPointer
if? command="[" -> 'vstack ++ instrPointer
else [
... |
Rewrite the snippet below in Arturo so it works the same as the original C code. |
#include <stdio.h>
int Gcd(int v1, int v2)
{
int a, b, r;
if (v1 < v2)
{
a = v2;
b = v1;
}
else
{
a = v1;
b = v2;
}
do
{
r = a % b;
if (r == 0)
{
break;
}
else
{
a = b;
b = r;
}
} while (1 == 1);
return b;
}
int NotInList(int num, int numtrip, int *tripletslist)
{
for... | lst: [1 2]
while [true][
n: 3
prev2: lst\[dec dec size lst]
prev1: last lst
while -> any? @[
contains? lst n
1 <> gcd @[n prev2]
1 <> gcd @[n prev1]
] -> n: n + 1
if n >= 50 -> break
'lst ++ n
]
loop split.every:10 lst 'a ->
print map a => [pad to :string & 3]... |
Maintain the same structure and functionality when rewriting this code in Arturo. |
#include <stdio.h>
int Gcd(int v1, int v2)
{
int a, b, r;
if (v1 < v2)
{
a = v2;
b = v1;
}
else
{
a = v1;
b = v2;
}
do
{
r = a % b;
if (r == 0)
{
break;
}
else
{
a = b;
b = r;
}
} while (1 == 1);
return b;
}
int NotInList(int num, int numtrip, int *tripletslist)
{
for... | lst: [1 2]
while [true][
n: 3
prev2: lst\[dec dec size lst]
prev1: last lst
while -> any? @[
contains? lst n
1 <> gcd @[n prev2]
1 <> gcd @[n prev1]
] -> n: n + 1
if n >= 50 -> break
'lst ++ n
]
loop split.every:10 lst 'a ->
print map a => [pad to :string & 3]... |
Write a version of this C function in Arturo with identical behavior. | #include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <locale.h>
uint64_t modPow(uint64_t base, uint64_t exp, uint64_t mod) {
if (mod == 1) return 0;
uint64_t result = 1;
base %= mod;
for (; exp > 0; exp >>= 1) {
if ((exp & 1) == 1) result = (result * base) % mod;
base = ... | curzon?: function [n,base]->
zero? (inc base^n) % inc base*n
first50: function [b][
result: new []
i: 1
while [50 > size result][
if curzon? i b -> 'result ++ i
i: i + 1
]
return result
]
oneThousandth: function [b][
cnt: 0
i: 1
while [cnt < 1000][
if curzo... |
Write the same code in Arturo as shown below in C. | #include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <locale.h>
uint64_t modPow(uint64_t base, uint64_t exp, uint64_t mod) {
if (mod == 1) return 0;
uint64_t result = 1;
base %= mod;
for (; exp > 0; exp >>= 1) {
if ((exp & 1) == 1) result = (result * base) % mod;
base = ... | curzon?: function [n,base]->
zero? (inc base^n) % inc base*n
first50: function [b][
result: new []
i: 1
while [50 > size result][
if curzon? i b -> 'result ++ i
i: i + 1
]
return result
]
oneThousandth: function [b][
cnt: 0
i: 1
while [cnt < 1000][
if curzo... |
Convert this C snippet to Arturo and keep its semantics consistent. | #include <stdio.h>
#include <stdlib.h>
int* mertens_numbers(int max) {
int* m = malloc((max + 1) * sizeof(int));
if (m == NULL)
return m;
m[1] = 1;
for (int n = 2; n <= max; ++n) {
m[n] = 1;
for (int k = 2; k <= n; ++k)
m[n] -= m[n/k];
}
return m;
}
int main... | mobius: function [n][
if n=0 -> return ""
if n=1 -> return 1
f: factors.prime n
if f <> unique f -> return 0
if? odd? size f -> return neg 1
else -> return 1
]
mertens: function [z][sum map 1..z => mobius]
print "The first 99 Mertens numbers are:"
loop split.every:20 [""]++map 1..99 => merten... |
Maintain the same structure and functionality when rewriting this code in Arturo. | #include <math.h>
#include <stdio.h>
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1;
unsigned int p;
for (; (n & 1) == 0; n >>= 1) {
++total;
}
for (p = 3; p * p <= n; p += 2) {
unsigned int count = 1;
for (; n % p == 0; n /= p) {
... | loop split.every:5 to [:string] map 1..50 => [product factors &] 'line [
print map line 'i -> pad i 10
]
|
Port the following code from C to Arturo with equivalent syntax and logic. | #include <math.h>
#include <stdio.h>
unsigned int divisor_count(unsigned int n) {
unsigned int total = 1;
unsigned int p;
for (; (n & 1) == 0; n >>= 1) {
++total;
}
for (p = 3; p * p <= n; p += 2) {
unsigned int count = 1;
for (; n % p == 0; n /= p) {
... | loop split.every:5 to [:string] map 1..50 => [product factors &] 'line [
print map line 'i -> pad i 10
]
|
Can you help me rewrite this code in Arturo instead of C, keeping it the same logically? | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#define LIMIT 1000000
#define LOWER_LIMIT 2500
bool *sieve(int limit) {
int i, p;
limit++;
bool *c = calloc(limit, sizeof(bool));
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = ... | factorials: map 1..20 => [product 1..&]
erdos?: function [x][
if not? prime? x -> return false
loop factorials 'f [
if f >= x -> break
if prime? x - f -> return false
]
return true
]
loop split.every:10 select 2..2500 => erdos? 'a ->
print map a => [pad to :string & 5]
|
Convert the following code from C to Arturo, ensuring the logic remains intact. | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#define LIMIT 1000000
#define LOWER_LIMIT 2500
bool *sieve(int limit) {
int i, p;
limit++;
bool *c = calloc(limit, sizeof(bool));
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = ... | factorials: map 1..20 => [product 1..&]
erdos?: function [x][
if not? prime? x -> return false
loop factorials 'f [
if f >= x -> break
if prime? x - f -> return false
]
return true
]
loop split.every:10 select 2..2500 => erdos? 'a ->
print map a => [pad to :string & 5]
|
Translate this program into Arturo but keep the logic exactly as in C. | #include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int locale_ok = 0;
wchar_t s_suits[] = L"♠♥♦♣";
const char *s_suits_ascii[] = { "S", "H", "D", "C" };
const char *s_nums[] = { "WHAT",
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
"OVERFLOW"
};
typedef struct { int suit, number, _s;... |
Red [Title: "Playing Cards"]
pip: ["a" "2" "3" "4" "5" "6" "7" "8" "9" "10" "j" "q" "k"]
suit: ["♣" "♦" "♥" "♠"]
make-deck: function [] [
new-deck: make block! 52
foreach s suit [foreach p pip [append/only new-deck reduce [p s]]]
return new-deck
]
shuffle: function [deck [block!]] [deck: random deck]
deal: fun... |
Write the same algorithm in Arturo as shown in this C implementation. | main(){printf("Code Golf");}
| prints"Code Golf"
prints join to[:char]digits.base:112 1683633059109764774
prints join to[:char][67 111 100 101 32 71 111 108 102]
|
Convert this C block to Arturo, preserving its control flow and logic. | main(){printf("Code Golf");}
| prints"Code Golf"
prints join to[:char]digits.base:112 1683633059109764774
prints join to[:char][67 111 100 101 32 71 111 108 102]
|
Write a version of this C function in Arturo with identical behavior. | #include <stdio.h>
int gcd(int a, int b) {
int c;
while (b) {
c = a;
a = b;
b = c % b;
}
return a;
}
struct pair {
int x, y;
};
void printPair(struct pair const *p) {
printf("{%d, %d}\n", p->x, p->y);
}
int main() {
struct pair pairs[] = {
{21,15}, {17,23}... | coprimes?: function [a b] -> 1 = gcd @[a b]
loop [[21 15] [17 23] [36 12] [18 29] [60 15]] 'pair [
print [pair\0 "and" pair\1 "ara" (coprimes? pair\0 pair\1)? -> "coprimes." -> "not coprimes."]
]
|
Generate an equivalent Arturo version of this C code. | #include <stdio.h>
int gcd(int a, int b) {
int c;
while (b) {
c = a;
a = b;
b = c % b;
}
return a;
}
struct pair {
int x, y;
};
void printPair(struct pair const *p) {
printf("{%d, %d}\n", p->x, p->y);
}
int main() {
struct pair pairs[] = {
{21,15}, {17,23}... | coprimes?: function [a b] -> 1 = gcd @[a b]
loop [[21 15] [17 23] [36 12] [18 29] [60 15]] 'pair [
print [pair\0 "and" pair\1 "ara" (coprimes? pair\0 pair\1)? -> "coprimes." -> "not coprimes."]
]
|
Translate this program into Arturo but keep the logic exactly as in C. | #include<stdlib.h>
#include<stdio.h>
long totient(long n){
long tot = n,i;
for(i=2;i*i<=n;i+=2){
if(n%i==0){
while(n%i==0)
n/=i;
tot-=tot/i;
}
if(i==2)
i=1;
}
if(n>1)
tot-=tot/n;
return tot;
}
long* perfectTotients(long n){
long *ptList = (long*)malloc(n*sizeof(long)), m,count=0,su... | totient: function [n][
tt: new n
nn: new n
i: new 2
while [nn >= i ^ 2][
if zero? nn % i [
while [zero? nn % i]->
'nn / i
'tt - tt/i
]
if i = 2 ->
i: new 1
'i + 2
]
if nn > 1 ->
'tt - tt/nn
return... |
Convert this C snippet to Arturo and keep its semantics consistent. | #include<stdlib.h>
#include<stdio.h>
long totient(long n){
long tot = n,i;
for(i=2;i*i<=n;i+=2){
if(n%i==0){
while(n%i==0)
n/=i;
tot-=tot/i;
}
if(i==2)
i=1;
}
if(n>1)
tot-=tot/n;
return tot;
}
long* perfectTotients(long n){
long *ptList = (long*)malloc(n*sizeof(long)), m,count=0,su... | totient: function [n][
tt: new n
nn: new n
i: new 2
while [nn >= i ^ 2][
if zero? nn % i [
while [zero? nn % i]->
'nn / i
'tt - tt/i
]
if i = 2 ->
i: new 1
'i + 2
]
if nn > 1 ->
'tt - tt/nn
return... |
Port the following code from C to Arturo with equivalent syntax and logic. | #include <stdio.h>
#include <gmp.h>
int a(unsigned int n) {
int k;
mpz_t p;
mpz_init_set_ui(p, 1);
mpz_mul_2exp(p, p, 1 << n);
mpz_sub_ui(p, p, 1);
for (k = 1; ; k += 2) {
if (mpz_probab_prime_p(p, 15) > 0) return k;
mpz_sub_ui(p, p, 2);
}
}
int main() {
unsigned int n;... | ultraUseful: function [n][
k: 1
p: (2^2^n) - k
while ø [
if prime? p -> return k
p: p-2
k: k+2
]
]
print [pad "n" 3 "|" pad.right "k" 4]
print repeat "-" 10
loop 1..10 'x ->
print [(pad to :string x 3) "|" (pad.right to :string ultraUseful x 4)]
|
Convert this C block to Arturo, preserving its control flow and logic. | #include <stdio.h>
#include <gmp.h>
int a(unsigned int n) {
int k;
mpz_t p;
mpz_init_set_ui(p, 1);
mpz_mul_2exp(p, p, 1 << n);
mpz_sub_ui(p, p, 1);
for (k = 1; ; k += 2) {
if (mpz_probab_prime_p(p, 15) > 0) return k;
mpz_sub_ui(p, p, 2);
}
}
int main() {
unsigned int n;... | ultraUseful: function [n][
k: 1
p: (2^2^n) - k
while ø [
if prime? p -> return k
p: p-2
k: k+2
]
]
print [pad "n" 3 "|" pad.right "k" 4]
print repeat "-" 10
loop 1..10 'x ->
print [(pad to :string x 3) "|" (pad.right to :string ultraUseful x 4)]
|
Port the provided C code into Arturo while preserving the original functionality. | #include <stdint.h>
#include <stdio.h>
uint64_t factorial(uint64_t n) {
uint64_t res = 1;
if (n == 0) return res;
while (n > 0) res *= n--;
return res;
}
uint64_t lah(uint64_t n, uint64_t k) {
if (k == 1) return factorial(n);
if (k == n) return 1;
if (k > n) return 0;
if (k < 1 || n < ... | factorial: function [n]-> product 1..n
lah: function [n,k][
if k=1 -> return factorial n
if k=n -> return 1
if k>n -> return 0
if or? k<1 n<1 -> return 0
return (((factorial n)*factorial n-1) / ((factorial k) * factorial k-1)) / factorial n-k
]
print @["n/k"] ++ map to [:string] 1..12 's -> pad s ... |
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include <stdint.h>
#include <stdio.h>
uint64_t factorial(uint64_t n) {
uint64_t res = 1;
if (n == 0) return res;
while (n > 0) res *= n--;
return res;
}
uint64_t lah(uint64_t n, uint64_t k) {
if (k == 1) return factorial(n);
if (k == n) return 1;
if (k > n) return 0;
if (k < 1 || n < ... | factorial: function [n]-> product 1..n
lah: function [n,k][
if k=1 -> return factorial n
if k=n -> return 1
if k>n -> return 0
if or? k<1 n<1 -> return 0
return (((factorial n)*factorial n-1) / ((factorial k) * factorial k-1)) / factorial n-k
]
print @["n/k"] ++ map to [:string] 1..12 's -> pad s ... |
Rewrite this program in Arturo while keeping its functionality equivalent to the C version. | #include<stdio.h>
int main()
{
int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
for(i=0;i<4;i++){
for(j=i+1;j<5;j++){
if(arr[i]+arr[j]==sum){
printf("[%d,%d]",i,j);
check = 1;
break;
}
}
}
if(check==0)
printf("[]");
return 0;
}
| twoSum: function [numbers, s][
loop.with:'i numbers 'x [
if not? null? j: <= index numbers s-x ->
return @[i j]
]
return []
]
nums: [0 2 11 19 90]
print ["twoSum 21:" twoSum nums 21]
print ["twoSum 25:" twoSum nums 25]
|
Please provide an equivalent version of this C code in Arturo. | #include<stdio.h>
int main()
{
int arr[5] = {0, 2, 11, 19, 90},sum = 21,i,j,check = 0;
for(i=0;i<4;i++){
for(j=i+1;j<5;j++){
if(arr[i]+arr[j]==sum){
printf("[%d,%d]",i,j);
check = 1;
break;
}
}
}
if(check==0)
printf("[]");
return 0;
}
| twoSum: function [numbers, s][
loop.with:'i numbers 'x [
if not? null? j: <= index numbers s-x ->
return @[i j]
]
return []
]
nums: [0 2 11 19 90]
print ["twoSum 21:" twoSum nums 21]
print ["twoSum 25:" twoSum nums 25]
|
Keep all operations the same but rewrite the snippet in Arturo. | #include <stdio.h>
#include <string.h>
void swap(char* p1, char* p2, size_t size) {
for (; size-- > 0; ++p1, ++p2) {
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
void cocktail_shaker_sort(void* base, size_t count, size_t size,
int (*cmp)(const void*, const void*)... | cocktailShiftSort: function [items][
a: new items
beginIdx: 0
endIdx: (size a)-2
while [beginIdx =< endIdx][
newBeginIdx: endIdx
newEndIdx: beginIdx
loop beginIdx..endIdx 'i [
if a\[i] > a\[i+1] [
tmp: a\[i]
a\[i]: a\[i+1]
... |
Convert this C snippet to Arturo and keep its semantics consistent. | #include<stdlib.h>
#include<stdio.h>
int
main ()
{
int i;
char *str = getenv ("LANG");
for (i = 0; str[i + 2] != 00; i++)
{
if ((str[i] == 'u' && str[i + 1] == 't' && str[i + 2] == 'f')
|| (str[i] == 'U' && str[i + 1] == 'T' && str[i + 2] == 'F'))
{
printf
("Uni... | canHandleUnicode?: function [][
any? @[
if key? env "LC_ALL" -> contains? lower get env "LC_ALL" "utf-8"
if key? env "LC_CTYPE" -> contains? lower get env "LC_CTYPE" "utf-8"
if key? env "LANG" -> contains? lower get env "LANG" "utf-8"
]
]
if? canHandleUnicode? ->
print "Ter... |
Convert this C block to Arturo, preserving its control flow and logic. | #include<stdlib.h>
#include<stdio.h>
int
main ()
{
int i;
char *str = getenv ("LANG");
for (i = 0; str[i + 2] != 00; i++)
{
if ((str[i] == 'u' && str[i + 1] == 't' && str[i + 2] == 'f')
|| (str[i] == 'U' && str[i + 1] == 'T' && str[i + 2] == 'F'))
{
printf
("Uni... | canHandleUnicode?: function [][
any? @[
if key? env "LC_ALL" -> contains? lower get env "LC_ALL" "utf-8"
if key? env "LC_CTYPE" -> contains? lower get env "LC_CTYPE" "utf-8"
if key? env "LANG" -> contains? lower get env "LANG" "utf-8"
]
]
if? canHandleUnicode? ->
print "Ter... |
Translate the given C code snippet into Arturo without altering its behavior. | #include <assert.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct bit_array_tag {
uint32_t size;
uint32_t* array;
} bit_array;
bool bit_array_create(bit_array* b, uint32_t size) {
uint32_t* array = calloc((size + 31)/32, sizeof(uint32_t)... | unprimeable?: function [n][
if prime? n -> return false
nd: to :string n
loop.with:'i nd 'prevDigit [
loop `0`..`9` 'newDigit [
if newDigit <> prevDigit [
nd\[i]: newDigit
if prime? to :integer nd -> return false
]
]
nd\[i]: pre... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.