Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Produce a language-to-language conversion: from C++ to Rust, same semantics. | #include <iostream>
#include <fstream>
#include <string>
#include <vector>
std::ostream& operator<<(std::ostream& out, const std::string s) {
return out << s.c_str();
}
struct gecos_t {
std::string fullname, office, extension, homephone, email;
friend std::ostream& operator<<(std::ostream&, const gecos_t... | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
Please provide an equivalent version of this C# code in Rust. | using System;
using System.IO;
namespace AppendPwdRosetta
{
class PasswordRecord
{
public string account, password, fullname, office, extension, homephone, email, directory, shell;
public int UID, GID;
public PasswordRecord(string account, string password, int UID, int GID, string fulln... | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
Can you help me rewrite this code in Rust instead of Java, keeping it the same logically? | import static java.util.Objects.requireNonNull;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
impor... | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
Generate an equivalent Python version of this Rust code. | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
passwd_list=[
dict(account='jsmith', password='x', UID=1001, GID=1000,
GECOS=dict(fullname='Joe Smith', office='Room 1007', extension='(234)555-8917',
homephone='(234)555-0077', email='jsmith@rosettacode.org'),
directory='/home/jsmith', shell='/bin/bash'),
dict(accoun... |
Produce a language-to-language conversion: from Rust to VB, same semantics. | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
$Include "Rapidq.inc"
dim file as qfilestream
dim filename as string
dim LogRec as string
filename = "C:\Logfile2.txt"
file.open(filename, fmCreate)
file.writeline "jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash"
file.writeline "jdoe:x:1002:1000:Jane... |
Change the following Go code into Rust without altering its purpose. | package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
)
type pw struct {
account, password string
uid, gid uint
gecos
directory, shell string
}
type gecos struct {
fullname, office, extension, homephone, email string
}
func (p *pw) encode(w io.Writer) (int, error... | use std::fs::File;
use std::fs::OpenOptions;
use std::io::BufRead;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result;
use std::io::Write;
use std::path::Path;
#[derive(Eq, PartialEq, Debug)]
pub struct PasswordRecord {
pub account: String,
pub password: String,
pub uid: u64,
pub gid:... |
Rewrite the snippet below in C# so it works the same as the original Ada code. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Translate this program into C but keep the logic exactly as in Ada. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Translate the given Ada code snippet into C++ without altering its behavior. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Write a version of this Ada function in Go with identical behavior. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Convert this Ada block to Java, preserving its control flow and logic. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Maintain the same structure and functionality when rewriting this code in Python. | with Ada.Text_IO, Generic_Root; use Generic_Root;
procedure Multiplicative_Root is
procedure Compute is new Compute_Root("*");
package TIO renames Ada.Text_IO;
package NIO is new TIO.Integer_IO(Number);
procedure Print_Numbers(Target_Root: Number; How_Many: Natural) is
Current: Number ... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Port the following code from Arturo to C with equivalent syntax and logic. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Rewrite the snippet below in C# so it works the same as the original Arturo code. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Maintain the same structure and functionality when rewriting this code in C++. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Change the programming language of this snippet from Arturo to Java without modifying what it does. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Produce a language-to-language conversion: from Arturo to Python, same semantics. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Write the same algorithm in Go as shown in this Arturo implementation. | Red ["Multiplicative digital root"]
mdr: function [
"Returns a block containing the mdr and persistence of an integer"
n [integer!]
][
persistence: 0
while [n > 10][
product: 1
m: n
while [m > 0][
product: m % 10 * product
m: to-integer m / 10
]
... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Ensure the translated C code behaves exactly like the original AWK snippet. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Rewrite this program in C# while keeping its functionality equivalent to the AWK version. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Convert this AWK block to C++, preserving its control flow and logic. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Maintain the same structure and functionality when rewriting this code in Java. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Convert this AWK snippet to Python and keep its semantics consistent. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Generate an equivalent Go version of this AWK code. |
BEGIN {
printMdrAndMp( 123321 );
printMdrAndMp( 7739 );
printMdrAndMp( 893 );
printMdrAndMp( 899998 );
tabulateMdr( 5 );
}
function printMdrAndMp( n )
{
calculateMdrAndMp( n );
printf( "%6d: MDR: %d, MP: %2d\n", n, MDR, MP );
}
function calculateMdrAndMp( n, ... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Convert the following code from Common_Lisp to C, ensuring the logic remains intact. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Translate this program into C# but keep the logic exactly as in Common_Lisp. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Transform the following Common_Lisp implementation into C++, maintaining the same output and logic. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Ensure the translated Java code behaves exactly like the original Common_Lisp snippet. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Maintain the same structure and functionality when rewriting this code in Python. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Produce a language-to-language conversion: from Common_Lisp to Go, same semantics. | (defun mdr/p (n)
"Return a list with MDR and MP of n"
(if (< n 10)
(list n 0)
(mdr/p-aux n 1 1)))
(defun mdr/p-aux (n a c)
(cond ((and (zerop n) (< a 10)) (list a c))
((zerop n) (mdr/p-aux a 1 (+ c 1)))
(t (mdr/p-aux (floor n 10) (* (rem n 10) a) c))))
(defun first-n-number-for-each-root (n &optional... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Change the following D code into C without altering its purpose. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Change the following D code into C# without altering its purpose. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Keep all operations the same but rewrite the snippet in C++. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Ensure the translated Java code behaves exactly like the original D snippet. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Change the following D code into Python without altering its purpose. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Please provide an equivalent version of this D code in Go. | import std.stdio, std.algorithm, std.typecons, std.range, std.conv;
auto mdRoot(in int n) pure {
auto mdr = [n];
while (mdr.back > 9)
mdr ~= reduce!q{a * b}(1, mdr.back.text.map!(d => d - '0'));
return tuple(mdr.length - 1, mdr.back);
}
void main() {
"Number: (MP, MDR)\n===... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Write the same code in C as shown below in Elixir. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Port the provided Elixir code into C# while preserving the original functionality. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Please provide an equivalent version of this Elixir code in C++. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Change the following Elixir code into Java without altering its purpose. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Produce a language-to-language conversion: from Elixir to Python, same semantics. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Preserve the algorithm and functionality while converting the code from Elixir to Go. | defmodule Digital do
def mdroot(n), do: mdroot(n, 0)
defp mdroot(n, persist) when n < 10, do: {n, persist}
defp mdroot(n, persist), do: mdroot(product(n, 1), persist+1)
defp product(0, prod), do: prod
defp product(n, prod), do: product(div(n, 10), prod*rem(n, 10))
def task1(data) do
IO.puts "Nu... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Translate this program into C but keep the logic exactly as in F#. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Rewrite the snippet below in C# so it works the same as the original F# code. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Write the same code in C++ as shown below in F#. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Maintain the same structure and functionality when rewriting this code in Java. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Write the same code in Python as shown below in F#. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Please provide an equivalent version of this F# code in Go. |
let rec fG n g=if n=0 then g else fG(n/10)(g*(n%10))
let mdr n=let rec mdr n g=if n<10 then (n,g) else mdr(fG n 1)(g+1) in mdr n 0
[123321; 7739; 893; 899998] |> List.iter(fun i->let n,g=mdr i in printfn "%d has mdr=%d with persitance %d" i n g)
let fN g=Seq.initInfinite id|>Seq.filter((mdr>>fst>>(=)g))|>Seq.take 5
se... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Write the same code in C as shown below in Factor. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Convert this Factor snippet to C# and keep its semantics consistent. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Transform the following Factor implementation into C++, maintaining the same output and logic. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Rewrite this program in Java while keeping its functionality equivalent to the Factor version. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Rewrite the snippet below in Python so it works the same as the original Factor code. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Translate the given Factor code snippet into Go without altering its behavior. | USING: arrays formatting fry io kernel lists lists.lazy math
math.text.utils prettyprint sequences ;
IN: rosetta-code.multiplicative-digital-root
: mdr ( n -- {persistence,root} )
0 swap
[ 1 digit-groups dup length 1 > ] [ product [ 1 + ] dip ] while
dup empty? [ drop { 0 } ] when first 2array ;
: print-m... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Convert this Fortran block to C#, preserving its control flow and logic. |
program mdr
implicit none
integer :: i, mdr, mp, n, j
character(len=*), parameter :: hfmt = '(A18)', nfmt = '(I6)'
character(len=*), parameter :: cfmt = '(A3)', rfmt = '(I3)', ffmt = '(I9)'
write(*,hfmt) 'Number MDR MP '
write(*,*) '------------------'
i = 123321
call root_pers(i,mdr,mp)
write(*,nfmt,advance='no... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Ensure the translated C++ code behaves exactly like the original Fortran snippet. |
program mdr
implicit none
integer :: i, mdr, mp, n, j
character(len=*), parameter :: hfmt = '(A18)', nfmt = '(I6)'
character(len=*), parameter :: cfmt = '(A3)', rfmt = '(I3)', ffmt = '(I9)'
write(*,hfmt) 'Number MDR MP '
write(*,*) '------------------'
i = 123321
call root_pers(i,mdr,mp)
write(*,nfmt,advance='no... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Port the following code from Fortran to C with equivalent syntax and logic. |
program mdr
implicit none
integer :: i, mdr, mp, n, j
character(len=*), parameter :: hfmt = '(A18)', nfmt = '(I6)'
character(len=*), parameter :: cfmt = '(A3)', rfmt = '(I3)', ffmt = '(I9)'
write(*,hfmt) 'Number MDR MP '
write(*,*) '------------------'
i = 123321
call root_pers(i,mdr,mp)
write(*,nfmt,advance='no... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Convert this Fortran block to Java, preserving its control flow and logic. |
program mdr
implicit none
integer :: i, mdr, mp, n, j
character(len=*), parameter :: hfmt = '(A18)', nfmt = '(I6)'
character(len=*), parameter :: cfmt = '(A3)', rfmt = '(I3)', ffmt = '(I9)'
write(*,hfmt) 'Number MDR MP '
write(*,*) '------------------'
i = 123321
call root_pers(i,mdr,mp)
write(*,nfmt,advance='no... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Maintain the same structure and functionality when rewriting this code in Python. |
program mdr
implicit none
integer :: i, mdr, mp, n, j
character(len=*), parameter :: hfmt = '(A18)', nfmt = '(I6)'
character(len=*), parameter :: cfmt = '(A3)', rfmt = '(I3)', ffmt = '(I9)'
write(*,hfmt) 'Number MDR MP '
write(*,*) '------------------'
i = 123321
call root_pers(i,mdr,mp)
write(*,nfmt,advance='no... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Produce a language-to-language conversion: from Haskell to C, same semantics. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Transform the following Haskell implementation into C#, maintaining the same output and logic. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Rewrite this program in C++ while keeping its functionality equivalent to the Haskell version. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Produce a functionally identical Java code for the snippet given in Haskell. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Rewrite this program in Python while keeping its functionality equivalent to the Haskell version. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Convert this Haskell block to Go, preserving its control flow and logic. | import Control.Arrow
import Data.Array
import Data.LazyArray
import Data.List (unfoldr)
import Data.Tuple
import Text.Printf
mpmdr :: Integer -> (Int, Integer)
mpmdr = (length *** head) . span (> 9) . iterate (product . digits)
mdrNums :: Int -> [(Integer, [Integer])]
mdrNums k = assocs $ lArrayMap (take k) (0,9)... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Convert the following code from J to C, ensuring the logic remains intact. | 10&#.inv 123321
1 2 3 3 2 1
| #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Convert this J snippet to C# and keep its semantics consistent. | 10&#.inv 123321
1 2 3 3 2 1
| using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Port the following code from J to C++ with equivalent syntax and logic. | 10&#.inv 123321
1 2 3 3 2 1
| #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Rewrite the snippet below in Java so it works the same as the original J code. | 10&#.inv 123321
1 2 3 3 2 1
| import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Convert this J snippet to Python and keep its semantics consistent. | 10&#.inv 123321
1 2 3 3 2 1
| try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Ensure the translated Go code behaves exactly like the original J snippet. | 10&#.inv 123321
1 2 3 3 2 1
| package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Rewrite the snippet below in C so it works the same as the original Julia code. | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Generate a C# translation of this Julia snippet without changing its computational steps. | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Ensure the translated C++ code behaves exactly like the original Julia snippet. | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Maintain the same structure and functionality when rewriting this code in Java. | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Port the following code from Julia to Python with equivalent syntax and logic. | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Can you help me rewrite this code in Go instead of Julia, keeping it the same logically? | function digitalmultroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
ds = prod(digits(ds, bs))
pers += 1
end
return (pers, ds)
end
| package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Rewrite the snippet below in C so it works the same as the original Mathematica code. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Port the following code from Mathematica to C# with equivalent syntax and logic. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Rewrite the snippet below in C++ so it works the same as the original Mathematica code. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Keep all operations the same but rewrite the snippet in Java. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Maintain the same structure and functionality when rewriting this code in Python. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Generate an equivalent Go version of this Mathematica code. | ClearAll[mdr, mp, nums];
mdr[n_] := NestWhile[Times @@ IntegerDigits[#] &, n, # > 9 &];
mp[n_] := Length@NestWhileList[Times @@ IntegerDigits[#] &, n, # > 9 &] - 1;
TableForm[{#, mdr[#], mp[#]} & /@ {123321, 7739, 893, 899998},
TableHeadings -> {None, {"Number", "MDR", "MP"}}]
nums = ConstantArray[{}, 10];
For[i = 0,... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Convert this Nim block to C, preserving its control flow and logic. | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Generate a C# translation of this Nim snippet without changing its computational steps. | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Maintain the same structure and functionality when rewriting this code in C++. | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Can you help me rewrite this code in Java instead of Nim, keeping it the same logically? | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Maintain the same structure and functionality when rewriting this code in Python. | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Translate the given Nim code snippet into Go without altering its behavior. | import strutils, sequtils, sugar
proc mdroot(n: int): tuple[mp, mdr: int] =
var mdr = @[n]
while mdr[mdr.high] > 9:
var n = 1
for dig in $mdr[mdr.high]:
n *= parseInt($dig)
mdr.add n
(mdr.high, mdr[mdr.high])
for n in [123321, 7739, 893, 899998]:
echo align($n, 6)," ",mdroot(n)
echo ""
v... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Rewrite this program in C while keeping its functionality equivalent to the Pascal version. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Write a version of this Pascal function in C# with identical behavior. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Produce a functionally identical C++ code for the snippet given in Pascal. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Write a version of this Pascal function in Java with identical behavior. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Ensure the translated Python code behaves exactly like the original Pascal snippet. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Port the provided Pascal code into Go while preserving the original functionality. | program MultRoot;
uses
sysutils;
type
tMul3Dgt = array[0..999] of Uint32;
tMulRoot = record
mrNum,
mrMul,
mrPers : Uint64;
end;
const
Testnumbers : array[0..16] of Uint64 =(123321,7739,893,899998,
1844674... | package main
import "fmt"
func mult(n uint64, base int) (mult uint64) {
for mult = 1; mult > 0 && n > 0; n /= uint64(base) {
mult *= n % uint64(base)
}
return
}
func MultDigitalRoot(n uint64, base int) (mp, mdr int) {
var m uint64
for m = n; m >= uint64(base); mp++ {
m = mult(m, base)
}
return mp, int(m... |
Can you help me rewrite this code in C instead of Perl, keeping it the same logically? | use warnings;
use strict;
sub mdr {
my $n = shift;
my($count, $mdr) = (0, $n);
while ($mdr > 9) {
my($m, $dm) = ($mdr, 1);
while ($m) {
$dm *= $m % 10;
$m = int($m/10);
}
$mdr = $dm;
$count++;
}
($count, $mdr);
}
print "Number: (MP, MDR)\n====== =========\n";
foreach my $n (... | #include <stdio.h>
#define twidth 5
#define mdr(rmdr, rmp, n)\
do { *rmp = 0; _mdr(rmdr, rmp, n); } while (0)
void _mdr(int *rmdr, int *rmp, long long n)
{
int r = n ? 1 : 0;
while (n) {
r *= (n % 10);
n /= 10;
}
(*rmp)++;
if (r >= 10)
_mdr(rmdr, rmp, r);
el... |
Write the same algorithm in C# as shown in this Perl implementation. | use warnings;
use strict;
sub mdr {
my $n = shift;
my($count, $mdr) = (0, $n);
while ($mdr > 9) {
my($m, $dm) = ($mdr, 1);
while ($m) {
$dm *= $m % 10;
$m = int($m/10);
}
$mdr = $dm;
$count++;
}
($count, $mdr);
}
print "Number: (MP, MDR)\n====== =========\n";
foreach my $n (... | using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static Tuple<int, int> DigitalRoot(long num)
{
int mp = 0;
while (num > 9)
{
num = num.ToString().ToCharArray().Select(x => x - '0').Aggregate((a, b) => a * b);
mp++;
}
... |
Generate an equivalent C++ version of this Perl code. | use warnings;
use strict;
sub mdr {
my $n = shift;
my($count, $mdr) = (0, $n);
while ($mdr > 9) {
my($m, $dm) = ($mdr, 1);
while ($m) {
$dm *= $m % 10;
$m = int($m/10);
}
$mdr = $dm;
$count++;
}
($count, $mdr);
}
print "Number: (MP, MDR)\n====== =========\n";
foreach my $n (... | #include <iomanip>
#include <map>
#include <vector>
#include <iostream>
using namespace std;
void calcMDR( int n, int c, int& a, int& b )
{
int m = n % 10; n /= 10;
while( n )
{
m *= ( n % 10 );
n /= 10;
}
if( m >= 10 ) calcMDR( m, ++c, a, b );
else { a = m; b = c; }
}
void table()
{
map... |
Convert the following code from Perl to Java, ensuring the logic remains intact. | use warnings;
use strict;
sub mdr {
my $n = shift;
my($count, $mdr) = (0, $n);
while ($mdr > 9) {
my($m, $dm) = ($mdr, 1);
while ($m) {
$dm *= $m % 10;
$m = int($m/10);
}
$mdr = $dm;
$count++;
}
($count, $mdr);
}
print "Number: (MP, MDR)\n====== =========\n";
foreach my $n (... | import java.util.*;
public class MultiplicativeDigitalRoot {
public static void main(String[] args) {
System.out.println("NUMBER MDR MP");
for (long n : new long[]{123321, 7739, 893, 899998}) {
long[] a = multiplicativeDigitalRoot(n);
System.out.printf("%6d %4d %4d%n", ... |
Generate an equivalent Python version of this Perl code. | use warnings;
use strict;
sub mdr {
my $n = shift;
my($count, $mdr) = (0, $n);
while ($mdr > 9) {
my($m, $dm) = ($mdr, 1);
while ($m) {
$dm *= $m % 10;
$m = int($m/10);
}
$mdr = $dm;
$count++;
}
($count, $mdr);
}
print "Number: (MP, MDR)\n====== =========\n";
foreach my $n (... | try:
from functools import reduce
except:
pass
def mdroot(n):
'Multiplicative digital root'
mdr = [n]
while mdr[-1] > 9:
mdr.append(reduce(int.__mul__, (int(dig) for dig in str(mdr[-1])), 1))
return len(mdr) - 1, mdr[-1]
if __name__ == '__main__':
print('Number: (MP, MDR)\n====== ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.