Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Generate a Go translation of this Erlang snippet without changing its computational steps.
is_perfect(X) -> X == lists:sum([N || N <- lists:seq(1,X-1), X rem N == 0]).
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Produce a language-to-language conversion: from F# to C, same semantics.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Port the provided F# code into C# while preserving the original functionality.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Produce a functionally identical C++ code for the snippet given in F#.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Translate this program into Java but keep the logic exactly as in F#.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Translate this program into Python but keep the logic exactly as in F#.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Port the following code from F# to VB with equivalent syntax and logic.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Convert the following code from F# to Go, ensuring the logic remains intact.
let perf n = n = List.fold (+) 0 (List.filter (fun i -> n % i = 0) [1..(n-1)]) for i in 1..10000 do if (perf i) then printfn "%i is perfect" i
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Port the following code from Factor to C with equivalent syntax and logic.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Transform the following Factor implementation into C#, maintaining the same output and logic.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Rewrite this program in C++ while keeping its functionality equivalent to the Factor version.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Write the same code in Java as shown below in Factor.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Convert this Factor block to Python, preserving its control flow and logic.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Write the same algorithm in VB as shown in this Factor implementation.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Write the same algorithm in Go as shown in this Factor implementation.
USING: kernel math math.primes.factors sequences ; IN: rosettacode.perfect-numbers : perfect? ( n -- ? ) [ divisors sum ] [ 2 * ] bi = ;
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Port the following code from Forth to C with equivalent syntax and logic.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Change the programming language of this snippet from Forth to C# without modifying what it does.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Change the following Forth code into C++ without altering its purpose.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Transform the following Forth implementation into Java, maintaining the same output and logic.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Maintain the same structure and functionality when rewriting this code in Python.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Produce a functionally identical VB code for the snippet given in Forth.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Maintain the same structure and functionality when rewriting this code in Go.
: perfect? 1 over 2/ 1+ 2 ?do over i mod 0= if i + then loop = ;
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Write the same code in C# as shown below in Fortran.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Write the same algorithm in C++ as shown in this Fortran implementation.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Change the programming language of this snippet from Fortran to C without modifying what it does.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Change the following Fortran code into Java without altering its purpose.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Write a version of this Fortran function in Python with identical behavior.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Keep all operations the same but rewrite the snippet in VB.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Convert this Fortran snippet to PHP and keep its semantics consistent.
FUNCTION isPerfect(n) LOGICAL :: isPerfect INTEGER, INTENT(IN) :: n INTEGER :: i, factorsum isPerfect = .FALSE. factorsum = 1 DO i = 2, INT(SQRT(REAL(n))) IF(MOD(n, i) == 0) factorsum = factorsum + i + (n / i) END DO IF (factorsum == n) isPerfect = .TRUE. END FUNCTION isPerfect
function is_perfect($number) { $sum = 0; for($i = 1; $i < $number; $i++) { if($number % $i == 0) $sum += $i; } return $sum == $number; } echo "Perfect numbers from 1 to 33550337:" . PHP_EOL; for($num = 1; $num < 33550337; $num++) { if(is_perfect($num)) echo $num . PHP_EOL; }
Generate a C translation of this Groovy snippet without changing its computational steps.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Convert the following code from Groovy to C#, ensuring the logic remains intact.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Preserve the algorithm and functionality while converting the code from Groovy to C++.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Transform the following Groovy implementation into Java, maintaining the same output and logic.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Rewrite the snippet below in Python so it works the same as the original Groovy code.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Preserve the algorithm and functionality while converting the code from Groovy to VB.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Produce a language-to-language conversion: from Groovy to Go, same semantics.
def isPerfect = { n -> n > 4 && (n == (2..Math.sqrt(n)).findAll { n % it == 0 }.inject(1) { factorSum, i -> factorSum += i + n/i }) }
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Change the programming language of this snippet from Haskell to C without modifying what it does.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Change the following Haskell code into C# without altering its purpose.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Produce a language-to-language conversion: from Haskell to C++, same semantics.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Convert this Haskell snippet to Java and keep its semantics consistent.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Translate the given Haskell code snippet into Python without altering its behavior.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Rewrite the snippet below in VB so it works the same as the original Haskell code.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Generate a Go translation of this Haskell snippet without changing its computational steps.
perfect n = n == sum [i | i <- [1..n-1], n `mod` i == 0]
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Port the following code from Icon to C with equivalent syntax and logic.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Transform the following Icon implementation into C#, maintaining the same output and logic.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Change the programming language of this snippet from Icon to C++ without modifying what it does.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Keep all operations the same but rewrite the snippet in Java.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Convert this Icon block to Python, preserving its control flow and logic.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Change the programming language of this snippet from Icon to VB without modifying what it does.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Write the same algorithm in Go as shown in this Icon implementation.
procedure main(arglist) limit := \arglist[1] | 100000 write("Perfect numbers from 1 to ",limit,":") every write(isperfect(1 to limit)) write("Done.") end procedure isperfect(n) local sum,i every (sum := 0) +:= (n ~= divisors(n)) if sum = n then return n end link factors
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Write the same code in C as shown below in J.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Generate an equivalent C# version of this J code.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Convert the following code from J to C++, ensuring the logic remains intact.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Generate an equivalent Java version of this J code.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Produce a language-to-language conversion: from J to Python, same semantics.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Maintain the same structure and functionality when rewriting this code in VB.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Translate the given J code snippet into Go without altering its behavior.
is_perfect=: +: = >:@#.~/.~&.q:@(6>.<.)
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Change the programming language of this snippet from Julia to C without modifying what it does.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Convert the following code from Julia to C++, ensuring the logic remains intact.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Rewrite the snippet below in Java so it works the same as the original Julia code.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Write the same code in Python as shown below in Julia.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Port the following code from Julia to VB with equivalent syntax and logic.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Transform the following Julia implementation into Go, maintaining the same output and logic.
isperfect(n::Integer) = n == sum([n % i == 0 ? i : 0 for i = 1:(n - 1)]) perfects(n::Integer) = filter(isperfect, 1:n) @show perfects(10000)
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Produce a language-to-language conversion: from Lua to C, same semantics.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Write the same code in C# as shown below in Lua.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Produce a language-to-language conversion: from Lua to C++, same semantics.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Write the same algorithm in Java as shown in this Lua implementation.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Rewrite this program in Python while keeping its functionality equivalent to the Lua version.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Translate the given Lua code snippet into VB without altering its behavior.
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Can you help me rewrite this code in Go instead of Lua, keeping it the same logically?
function isPerfect(x) local sum = 0 for i = 1, x-1 do sum = (x % i) == 0 and sum + i or sum end return sum == x end
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Write the same algorithm in C as shown in this Mathematica implementation.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Write the same code in C# as shown below in Mathematica.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Generate a C++ translation of this Mathematica snippet without changing its computational steps.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Translate the given Mathematica code snippet into Java without altering its behavior.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Convert the following code from Mathematica to Python, ensuring the logic remains intact.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Write the same algorithm in VB as shown in this Mathematica implementation.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Transform the following Mathematica implementation into Go, maintaining the same output and logic.
PerfectQ[i_Integer] := Total[Divisors[i]] == 2 i
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Convert this MATLAB snippet to C and keep its semantics consistent.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Change the following MATLAB code into C# without altering its purpose.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Ensure the translated C++ code behaves exactly like the original MATLAB snippet.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Keep all operations the same but rewrite the snippet in Java.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Preserve the algorithm and functionality while converting the code from MATLAB to Python.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Rewrite the snippet below in VB so it works the same as the original MATLAB code.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Convert this MATLAB snippet to Go and keep its semantics consistent.
function perf = isPerfect(n) total = 0; for k = 1:n-1 if ~mod(n, k) total = total+k; end end perf = total == n; end
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Write a version of this Nim function in C with identical behavior.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Port the following code from Nim to C# with equivalent syntax and logic.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Change the programming language of this snippet from Nim to C++ without modifying what it does.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
#include <iostream> using namespace std ; int divisor_sum( int number ) { int sum = 0 ; for ( int i = 1 ; i < number ; i++ ) if ( number % i == 0 ) sum += i ; return sum; } int main( ) { cout << "Perfect numbers from 1 to 33550337:\n" ; for ( int num = 1 ; num < 33550337 ; num++ ) { if (divisor_sum(num) == num) cout << num << '\n' ; } return 0 ; }
Keep all operations the same but rewrite the snippet in Java.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Change the following Nim code into Python without altering its purpose.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Transform the following Nim implementation into VB, maintaining the same output and logic.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Port the provided Nim code into Go while preserving the original functionality.
import math proc isPerfect(n: int): bool = var sum: int = 1 for d in 2 .. int(n.toFloat.sqrt): if n mod d == 0: inc sum, d let q = n div d if q != d: inc sum, q result = n == sum for n in 2..10_000: if n.isPerfect: echo n
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Translate this program into C but keep the logic exactly as in OCaml.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Generate an equivalent C# version of this OCaml code.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }
Write the same code in Java as shown below in OCaml.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
public static boolean perf(int n){ int sum= 0; for(int i= 1;i < n;i++){ if(n % i == 0){ sum+= i; } } return sum == n; }
Rewrite this program in Python while keeping its functionality equivalent to the OCaml version.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
def perf1(n): sum = 0 for i in range(1, n): if n % i == 0: sum += i return sum == n
Change the programming language of this snippet from OCaml to VB without modifying what it does.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
Private Function Factors(x As Long) As String Application.Volatile Dim i As Long Dim cooresponding_factors As String Factors = 1 corresponding_factors = x For i = 2 To Sqr(x) If x Mod i = 0 Then Factors = Factors & ", " & i If i <> x / i Then corresponding_factors = x / i & ", " & corresponding_factors End If Next i If x <> 1 Then Factors = Factors & ", " & corresponding_factors End Function Private Function is_perfect(n As Long) fs = Split(Factors(n), ", ") Dim f() As Long ReDim f(UBound(fs)) For i = 0 To UBound(fs) f(i) = Val(fs(i)) Next i is_perfect = WorksheetFunction.Sum(f) - n = n End Function Public Sub main() Dim i As Long For i = 2 To 100000 If is_perfect(i) Then Debug.Print i Next i End Sub
Change the following OCaml code into Go without altering its purpose.
let perf n = let sum = ref 0 in for i = 1 to n-1 do if n mod i = 0 then sum := !sum + i done; !sum = n
package main import "fmt" func computePerfect(n int64) bool { var sum int64 for i := int64(1); i < n; i++ { if n%i == 0 { sum += i } } return sum == n } func isPerfect(n int64) bool { switch n { case 6, 28, 496, 8128, 33550336, 8589869056, 137438691328, 2305843008139952128: return true } return false } func main() { for n := int64(1); ; n++ { if isPerfect(n) != computePerfect(n) { panic("bug") } if n%1e3 == 0 { fmt.Println("tested", n) } } }
Maintain the same structure and functionality when rewriting this code in C.
program PerfectNumbers; function isPerfect(number: longint): boolean; var i, sum: longint; begin sum := 1; for i := 2 to round(sqrt(real(number))) do if (number mod i = 0) then sum := sum + i + (number div i); isPerfect := (sum = number); end; var candidate: longint; begin writeln('Perfect numbers from 1 to 33550337:'); for candidate := 2 to 33550337 do if isPerfect(candidate) then writeln (candidate, ' is a perfect number.'); end.
#include "stdio.h" #include "math.h" int perfect(int n) { int max = (int)sqrt((double)n) + 1; int tot = 1; int i; for (i = 2; i < max; i++) if ( (n % i) == 0 ) { tot += i; int q = n / i; if (q > i) tot += q; } return tot == n; } int main() { int n; for (n = 2; n < 33550337; n++) if (perfect(n)) printf("%d\n", n); return 0; }
Port the following code from Pascal to C# with equivalent syntax and logic.
program PerfectNumbers; function isPerfect(number: longint): boolean; var i, sum: longint; begin sum := 1; for i := 2 to round(sqrt(real(number))) do if (number mod i = 0) then sum := sum + i + (number div i); isPerfect := (sum = number); end; var candidate: longint; begin writeln('Perfect numbers from 1 to 33550337:'); for candidate := 2 to 33550337 do if isPerfect(candidate) then writeln (candidate, ' is a perfect number.'); end.
static void Main(string[] args) { Console.WriteLine("Perfect numbers from 1 to 33550337:"); for (int x = 0; x < 33550337; x++) { if (IsPerfect(x)) Console.WriteLine(x + " is perfect."); } Console.ReadLine(); } static bool IsPerfect(int num) { int sum = 0; for (int i = 1; i < num; i++) { if (num % i == 0) sum += i; } return sum == num ; }