Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Maintain the same structure and functionality when rewriting this code in C#. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... |
Change the following REXX code into C++ without altering its purpose. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... |
Write the same code in Java as shown below in REXX. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... |
Translate the given REXX code snippet into Python without altering its behavior. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Maintain the same structure and functionality when rewriting this code in VB. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Keep all operations the same but rewrite the snippet in Go. |
niner = '25 15 -5' ,
'15 18 0' ,
'-5 0 11'
call Cholesky niner
hexer = 18 22 54 42,
22 70 86 62,
54 86 174 134,
42 62 134 106
call Cholesky hex... | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... |
Translate this program into C but keep the logic exactly as in Ruby. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *cholesky(double *A, int n) {
double *L = (double*)calloc(n * n, sizeof(double));
if (L == NULL)
exit(EXIT_FAILURE);
for (int i = 0; i < n; i++)
for (int j = 0; j < (i+1); j++) {
double s = 0;
for (int ... |
Change the programming language of this snippet from Ruby to C# without modifying what it does. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... |
Preserve the algorithm and functionality while converting the code from Ruby to C++. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... |
Produce a language-to-language conversion: from Ruby to Java, same semantics. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... |
Convert the following code from Ruby to Python, ensuring the logic remains intact. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Maintain the same structure and functionality when rewriting this code in VB. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Write the same algorithm in Go as shown in this Ruby implementation. | require 'matrix'
class Matrix
def cholesky_factor
raise ArgumentError, "must provide symmetric matrix" unless symmetric?
l = Array.new(row_size) {Array.new(row_size, 0)}
(0 ... row_size).each do |k|
(0 ... row_size).each do |i|
if i == k
sum = (0 .. k-1).inject(0.0) {|sum, j| sum ... | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... |
Convert this Scala snippet to C and keep its semantics consistent. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *cholesky(double *A, int n) {
double *L = (double*)calloc(n * n, sizeof(double));
if (L == NULL)
exit(EXIT_FAILURE);
for (int i = 0; i < n; i++)
for (int j = 0; j < (i+1); j++) {
double s = 0;
for (int ... |
Rewrite this program in C# while keeping its functionality equivalent to the Scala version. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... |
Change the following Scala code into C++ without altering its purpose. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... |
Change the programming language of this snippet from Scala to Java without modifying what it does. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... |
Write the same code in Python as shown below in Scala. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Generate a VB translation of this Scala snippet without changing its computational steps. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Write a version of this Scala function in Go with identical behavior. |
fun cholesky(a: DoubleArray): DoubleArray {
val n = Math.sqrt(a.size.toDouble()).toInt()
val l = DoubleArray(a.size)
var s: Double
for (i in 0 until n)
for (j in 0 .. i) {
s = 0.0
for (k in 0 until j) s += l[i * n + k] * l[j * n + k]
l[i * n + j] = when {
... | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... |
Translate the given Swift code snippet into C without altering its behavior. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *cholesky(double *A, int n) {
double *L = (double*)calloc(n * n, sizeof(double));
if (L == NULL)
exit(EXIT_FAILURE);
for (int i = 0; i < n; i++)
for (int j = 0; j < (i+1); j++) {
double s = 0;
for (int ... |
Can you help me rewrite this code in C# instead of Swift, keeping it the same logically? | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... |
Produce a language-to-language conversion: from Swift to C++, same semantics. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... |
Produce a language-to-language conversion: from Swift to Java, same semantics. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... |
Convert this Swift snippet to Python and keep its semantics consistent. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Convert this Swift block to VB, preserving its control flow and logic. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Transform the following Swift implementation into Go, maintaining the same output and logic. | func cholesky(matrix: [Double], n: Int) -> [Double] {
var res = [Double](repeating: 0, count: matrix.count)
for i in 0..<n {
for j in 0..<i+1 {
var s = 0.0
for k in 0..<j {
s += res[i * n + k] * res[j * n + k]
}
if i == j {
res[i * n + j] = (matrix[i * n + i] - s).squa... | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... |
Rewrite the snippet below in C so it works the same as the original Tcl code. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *cholesky(double *A, int n) {
double *L = (double*)calloc(n * n, sizeof(double));
if (L == NULL)
exit(EXIT_FAILURE);
for (int i = 0; i < n; i++)
for (int j = 0; j < (i+1); j++) {
double s = 0;
for (int ... |
Write a version of this Tcl function in C# with identical behavior. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... |
Translate the given Tcl code snippet into C++ without altering its behavior. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... |
Convert this Tcl block to Java, preserving its control flow and logic. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... |
Maintain the same structure and functionality when rewriting this code in Python. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Convert the following code from Tcl to VB, ensuring the logic remains intact. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Port the provided Tcl code into Go while preserving the original functionality. | proc cholesky a {
set m [llength $a]
set n [llength [lindex $a 0]]
set l [lrepeat $m [lrepeat $n 0.0]]
for {set i 0} {$i < $m} {incr i} {
for {set k 0} {$k < $i+1} {incr k} {
set sum 0.0
for {set j 0} {$j < $k} {incr j} {
set sum [expr {$sum + [lindex $l $i $j] * [lindex $l $k $j]}]
}
... | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... |
Translate this program into Rust but keep the logic exactly as in C. | #include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *cholesky(double *A, int n) {
double *L = (double*)calloc(n * n, sizeof(double));
if (L == NULL)
exit(EXIT_FAILURE);
for (int i = 0; i < n; i++)
for (int j = 0; j < (i+1); j++) {
double s = 0;
for (int ... | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... |
Please provide an equivalent version of this C# code in Rust. | using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Cholesky
{
class Program
{
static void Main(string[] args)
{
double[,] test1 = new double[,]
{
{25, 15, -5},
... | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... |
Preserve the algorithm and functionality while converting the code from Java to Rust. | import java.util.Arrays;
public class Cholesky {
public static double[][] chol(double[][] a){
int m = a.length;
double[][] l = new double[m][m];
for(int i = 0; i< m;i++){
for(int k = 0; k < (i+1); k++){
double sum = 0;
for(int j = 0; j < k; j++){
sum += l[i][j] * l[k][j];
}
l[i][k] = (i... | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... |
Produce a functionally identical Python code for the snippet given in Rust. | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... | from __future__ import print_function
from pprint import pprint
from math import sqrt
def cholesky(A):
L = [[0.0] * len(A) for _ in xrange(len(A))]
for i in xrange(len(A)):
for j in xrange(i+1):
s = sum(L[i][k] * L[j][k] for k in xrange(j))
L[i][j] = sqrt(A[i][i] - s) if (i ==... |
Maintain the same structure and functionality when rewriting this code in VB. | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... | Function Cholesky(Mat As Range) As Variant
Dim A() As Double, L() As Double, sum As Double, sum2 As Double
Dim m As Byte, i As Byte, j As Byte, k As Byte
If Mat.Rows.Count <> Mat.Columns.Count Then
MsgBox ("Correlation matrix is not square")
Exit Function
End If
m = Mat.Rows.Count
... |
Ensure the translated Rust code behaves exactly like the original C++ snippet. | #include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
template <typename scalar_type> class matrix {
public:
matrix(size_t rows, size_t columns)
: rows_(rows), columns_(columns), elements_(rows * columns) {}
matrix(size_t rows, size_t columns, scalar_type value)
... | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... |
Write the same code in Rust as shown below in Go. | package main
import (
"fmt"
"math"
)
type symmetric struct {
order int
ele []float64
}
type lower struct {
order int
ele []float64
}
func (s *symmetric) print() {
const eleFmt = "%10.5f "
row, diag := 1, 0
for i, e := range s.ele {
fmt.Printf(eleFmt, e)
i... | fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
let mut res = vec![0.0; mat.len()];
for i in 0..n {
for j in 0..(i+1){
let mut s = 0.0;
for k in 0..j {
s += res[i * n + k] * res[j * n + k];
}
res[i * n + j] = if i == j { (mat[i * n + i] ... |
Produce a language-to-language conversion: from Ada to C#, same semantics. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Ensure the translated C code behaves exactly like the original Ada snippet. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Rewrite this program in C++ while keeping its functionality equivalent to the Ada version. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Transform the following Ada implementation into Go, maintaining the same output and logic. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Convert this Ada snippet to Java and keep its semantics consistent. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Write the same algorithm in Python as shown in this Ada implementation. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Rewrite this program in VB while keeping its functionality equivalent to the Ada version. | with Prime_Numbers, Ada.Text_IO;
procedure Test_Kth_Prime is
package Integer_Numbers is new
Prime_Numbers (Natural, 0, 1, 2);
use Integer_Numbers;
Out_Length: constant Positive := 10;
N: Positive;
begin
for K in 1 .. 5 loop
Ada.Text_IO.Put("K =" & Integer'Image(K) &": ");
... | Private Function kprime(ByVal n As Integer, k As Integer) As Boolean
Dim p As Integer, factors As Integer
p = 2
factors = 0
Do While factors < k And p * p <= n
Do While n Mod p = 0
n = n / p
factors = factors + 1
Loop
p = p + 1
Loop
factors = facto... |
Maintain the same structure and functionality when rewriting this code in C. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Produce a language-to-language conversion: from Arturo to C#, same semantics. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Change the following Arturo code into C++ without altering its purpose. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Change the following Arturo code into Java without altering its purpose. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Maintain the same structure and functionality when rewriting this code in Python. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Keep all operations the same but rewrite the snippet in VB. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Port the provided Arturo code into Go while preserving the original functionality. | almostPrime: function [k, listLen][
result: new []
test: 2
c: 0
while [c < listLen][
i: 2
m: 0
n: test
while [i =< n][
if? zero? n % i [
n: n / i
m: m + 1
]
else -> i: i + 1
]
if m = k [... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Transform the following AutoHotKey implementation into C, maintaining the same output and logic. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Generate a C# translation of this AutoHotKey snippet without changing its computational steps. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Keep all operations the same but rewrite the snippet in C++. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Write a version of this AutoHotKey function in Java with identical behavior. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Change the programming language of this snippet from AutoHotKey to Python without modifying what it does. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Write the same code in VB as shown below in AutoHotKey. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Produce a language-to-language conversion: from AutoHotKey to Go, same semantics. | kprime(n,k) {
p:=2, f:=0
while( (f<k) && (p*p<=n) ) {
while ( 0==mod(n,p) ) {
n/=p
f++
}
p++
}
return f + (n>1) == k
}
k:=1, results:=""
while( k<=5 ) {
i:=2, c:=0, results:=results "k =" k ":"
while( c<10 ) {
if (kprime(i,k)) {
results:=results " " i
c++
}
i++
}
results:=results "`n"
... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Port the following code from AWK to C with equivalent syntax and logic. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Write the same algorithm in C# as shown in this AWK implementation. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Port the provided AWK code into C++ while preserving the original functionality. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Translate this program into Java but keep the logic exactly as in AWK. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Preserve the algorithm and functionality while converting the code from AWK to Python. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Rewrite this program in VB while keeping its functionality equivalent to the AWK version. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Rewrite this program in Go while keeping its functionality equivalent to the AWK version. |
BEGIN {
for (k=1; k<=5; k++) {
printf("%d:",k)
c = 0
i = 1
while (c < 10) {
if (kprime(++i,k)) {
printf(" %d",i)
c++
}
}
printf("\n")
}
exit(0)
}
function kprime(n,k, f,p) {
for (p=2; f<k && p*p<=n; p++) {
while (n % p == 0)... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Produce a language-to-language conversion: from Clojure to C, same semantics. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Port the provided Clojure code into C# while preserving the original functionality. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Generate a C++ translation of this Clojure snippet without changing its computational steps. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Rewrite the snippet below in Java so it works the same as the original Clojure code. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Transform the following Clojure implementation into Python, maintaining the same output and logic. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Keep all operations the same but rewrite the snippet in VB. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Change the following Clojure code into Go without altering its purpose. | (ns clojure.examples.almostprime
(:gen-class))
(defn divisors [n]
" Finds divisors by looping through integers 2, 3,...i.. up to sqrt (n) [note: rather than compute sqrt(), test with i*i <=n] "
(let [div (some #(if (= 0 (mod n %)) % nil) (take-while #(<= (* % %) n) (iterate inc 2)))]
(if div ... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Produce a language-to-language conversion: from Common_Lisp to C, same semantics. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Convert the following code from Common_Lisp to C#, ensuring the logic remains intact. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Keep all operations the same but rewrite the snippet in C++. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Generate a Java translation of this Common_Lisp snippet without changing its computational steps. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Transform the following Common_Lisp implementation into Python, maintaining the same output and logic. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Change the following Common_Lisp code into VB without altering its purpose. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Translate the given Common_Lisp code snippet into Go without altering its behavior. | (defun start ()
(loop for k from 1 to 5
do (format t "k = ~a: ~a~%" k (collect-k-almost-prime k))))
(defun collect-k-almost-prime (k &optional (d 2) (lst nil))
(cond ((= (length lst) 10) (reverse lst))
((= (?-primality d) k) (collect-k-almost-prime k (+ d 1) (cons d lst)))
(t (collect-k-almost-... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Please provide an equivalent version of this D code in C. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Transform the following D implementation into C#, maintaining the same output and logic. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Keep all operations the same but rewrite the snippet in C++. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Maintain the same structure and functionality when rewriting this code in Java. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Can you help me rewrite this code in Python instead of D, keeping it the same logically? | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Port the provided D code into VB while preserving the original functionality. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Convert the following code from D to Go, ensuring the logic remains intact. | import std.stdio, std.algorithm, std.traits;
Unqual!T[] decompose(T)(in T number) pure nothrow
in {
assert(number > 1);
} body {
typeof(return) result;
Unqual!T n = number;
for (Unqual!T i = 2; n % i == 0; n /= i)
result ~= i;
for (Unqual!T i = 3; n >= i * i; i += 2)
for (; n % i =... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Write the same algorithm in C as shown in this Delphi implementation. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Change the following Delphi code into C# without altering its purpose. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Change the programming language of this snippet from Delphi to C++ without modifying what it does. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Rewrite the snippet below in Java so it works the same as the original Delphi code. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | public class AlmostPrime {
public static void main(String[] args) {
for (int k = 1; k <= 5; k++) {
System.out.print("k = " + k + ":");
for (int i = 2, c = 0; c < 10; i++) {
if (kprime(i, k)) {
System.out.print(" " + i);
c++;
... |
Can you help me rewrite this code in Python instead of Delphi, keeping it the same logically? | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | from prime_decomposition import decompose
from itertools import islice, count
try:
from functools import reduce
except:
pass
def almostprime(n, k=2):
d = decompose(n)
try:
terms = [next(d) for i in range(k)]
return reduce(int.__mul__, terms, 1) == n
except:
return False
... |
Port the following code from Delphi to VB with equivalent syntax and logic. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | for k = 1 to 5
print "k = "; k; " :";
i = 2
c = 0
while c < 10
if kPrime(i, k) then
print " "; using("###", i);
c = c +1
end if
i = i +1
wend
print
next k
end
function kPrime(n, k)
f = 0
for i = 2 to n
while n mod i = 0
... |
Write the same code in Go as shown below in Delphi. | program AlmostPrime;
function IsKPrime(const n, k: Integer): Boolean;
var
p, f, v: Integer;
begin
f := 0;
p := 2;
v := n;
while (f < k) and (p*p <= n) do begin
while (v mod p) = 0 do begin
v := v div p;
Inc(f);
end;
Inc(p);
end;
if v > 1 then Inc(f);
Result := f = k;
end;
var... | package main
import "fmt"
func kPrime(n, k int) bool {
nf := 0
for i := 2; i <= n; i++ {
for n%i == 0 {
if nf == k {
return false
}
nf++
n /= i
}
}
return nf == k
}
func gen(k, n int) []int {
r := make([]int, n)
n... |
Rewrite the snippet below in C so it works the same as the original Elixir code. | defmodule Factors do
def factors(n), do: factors(n,2,[])
defp factors(1,_,acc), do: acc
defp factors(n,k,acc) when rem(n,k)==0, do: factors(div(n,k),k,[k|acc])
defp factors(n,k,acc) , do: factors(n,k+1,acc)
def kfactors(n,k), do: kfactors(n,k,1,1,[])
defp kfactors(_tn,tk,_n,k,_acc) ... | #include <stdio.h>
int kprime(int n, int k)
{
int p, f = 0;
for (p = 2; f < k && p*p <= n; p++)
while (0 == n % p)
n /= p, f++;
return f + (n > 1) == k;
}
int main(void)
{
int i, c, k;
for (k = 1; k <= 5; k++) {
printf("k = %d:", k);
for (i = 2, c = 0; c < 10; i++)
if (kprime(i, k)) {
printf("... |
Rewrite this program in C# while keeping its functionality equivalent to the Elixir version. | defmodule Factors do
def factors(n), do: factors(n,2,[])
defp factors(1,_,acc), do: acc
defp factors(n,k,acc) when rem(n,k)==0, do: factors(div(n,k),k,[k|acc])
defp factors(n,k,acc) , do: factors(n,k+1,acc)
def kfactors(n,k), do: kfactors(n,k,1,1,[])
defp kfactors(_tn,tk,_n,k,_acc) ... | using System;
using System.Collections.Generic;
using System.Linq;
namespace AlmostPrime
{
class Program
{
static void Main(string[] args)
{
foreach (int k in Enumerable.Range(1, 5))
{
KPrime kprime = new KPrime() { K = k };
Console.WriteL... |
Preserve the algorithm and functionality while converting the code from Elixir to C++. | defmodule Factors do
def factors(n), do: factors(n,2,[])
defp factors(1,_,acc), do: acc
defp factors(n,k,acc) when rem(n,k)==0, do: factors(div(n,k),k,[k|acc])
defp factors(n,k,acc) , do: factors(n,k+1,acc)
def kfactors(n,k), do: kfactors(n,k,1,1,[])
defp kfactors(_tn,tk,_n,k,_acc) ... | #include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <list>
bool k_prime(unsigned n, unsigned k) {
unsigned f = 0;
for (unsigned p = 2; f < k && p * p <= n; p++)
while (0 == n % p) { n /= p; f++; }
return f + (n > 1 ? 1 : 0) == k;
}
std::list<unsigned> primes(unsig... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.