WeixiangYan/CodeTransOcean huggingface dataset:
Task: Apply a callback to an array








VB: Option Explicit

Sub Main()
Dim arr, i
    
    arr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    
    For i = LBound(arr) To UBound(arr): arr(i) = Fibonacci(arr(i)): Next
    
    
    Debug.Print Join(arr, ", ")
End Sub

Private Function Fibonacci(N) As Variant
    If N <= 1 Then
        Fibonacci = N
    Else
        Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
    End If
End Function
Task: Apply a callback to an array




Go: package main

import "fmt"

func main() {
    for _, i := range []int{1, 2, 3, 4, 5} {
        fmt.Println(i * i)
    }
}
Task: Apply a callback to an array



C#: int[] intArray = { 1, 2, 3, 4, 5 };

int[] squares1 = intArray.Select(x => x * x).ToArray();


int[] squares2 = (from x in intArray
                  select x * x).ToArray();


foreach (var i in intArray)
    Console.WriteLine(i * i);








Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array


C++: #include <iostream> 
#include <algorithm> 


void print_square(int i) {
  std::cout << i*i << " ";
}

int main() {
  
  int ary[]={1,2,3,4,5};
  
  std::for_each(ary,ary+5,print_square);
  return 0;
}










Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array

C: #ifndef CALLBACK_H
#define CALLBACK_H


void map(int* array, int len, void(*callback)(int,int));

#endif










Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array




Go: package main

import "fmt"

func main() {
    for _, i := range []int{1, 2, 3, 4, 5} {
        fmt.Println(i * i)
    }
}







Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array





Java: public class ArrayCallback7 {

    interface IntConsumer {
        void run(int x);
    }

    interface IntToInt {
        int run(int x);
    }

    static void forEach(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.run(i);
        }
    }

    static void update(int[] arr, IntToInt mapper) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = mapper.run(arr[i]);
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });

        update(numbers, new IntToInt() {
            @Override
            public int run(int x) {
                return x * x;
            }
        });

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });
    }
}






Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array







Python: def square(n):
    return n * n
  
numbers = [1, 3, 5, 7]

squares1 = [square(n) for n in numbers]     

squares2a = map(square, numbers)            

squares2b = map(lambda x: x*x, numbers)     

squares3 = [n * n for n in numbers]         
                                            

isquares1 = (n * n for n in numbers)        

import itertools
isquares2 = itertools.imap(square, numbers) 




Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array








VB: Option Explicit

Sub Main()
Dim arr, i
    
    arr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    
    For i = LBound(arr) To UBound(arr): arr(i) = Fibonacci(arr(i)): Next
    
    
    Debug.Print Join(arr, ", ")
End Sub

Private Function Fibonacci(N) As Variant
    If N <= 1 Then
        Fibonacci = N
    Else
        Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
    End If
End Function



Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array






PHP: function cube($n)
{
   return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);





Fortran: module arrCallback
contains
    elemental function cube( x )
        implicit none
        real :: cube
        real, intent(in) :: x
        cube = x * x * x
    end function cube
end module arrCallback
Task: Apply a callback to an array

C: #ifndef CALLBACK_H
#define CALLBACK_H


void map(int* array, int len, void(*callback)(int,int));

#endif









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array



C#: int[] intArray = { 1, 2, 3, 4, 5 };

int[] squares1 = intArray.Select(x => x * x).ToArray();


int[] squares2 = (from x in intArray
                  select x * x).ToArray();


foreach (var i in intArray)
    Console.WriteLine(i * i);







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array


C++: #include <iostream> 
#include <algorithm> 


void print_square(int i) {
  std::cout << i*i << " ";
}

int main() {
  
  int ary[]={1,2,3,4,5};
  
  std::for_each(ary,ary+5,print_square);
  return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array





Java: public class ArrayCallback7 {

    interface IntConsumer {
        void run(int x);
    }

    interface IntToInt {
        int run(int x);
    }

    static void forEach(int[] arr, IntConsumer consumer) {
        for (int i : arr) {
            consumer.run(i);
        }
    }

    static void update(int[] arr, IntToInt mapper) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = mapper.run(arr[i]);
        }
    }

    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });

        update(numbers, new IntToInt() {
            @Override
            public int run(int x) {
                return x * x;
            }
        });

        forEach(numbers, new IntConsumer() {
            public void run(int x) {
                System.out.println(x);
            }
        });
    }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array







Python: def square(n):
    return n * n
  
numbers = [1, 3, 5, 7]

squares1 = [square(n) for n in numbers]     

squares2a = map(square, numbers)            

squares2b = map(lambda x: x*x, numbers)     

squares3 = [n * n for n in numbers]         
                                            

isquares1 = (n * n for n in numbers)        

import itertools
isquares2 = itertools.imap(square, numbers) 



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array








VB: Option Explicit

Sub Main()
Dim arr, i
    
    arr = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

    
    For i = LBound(arr) To UBound(arr): arr(i) = Fibonacci(arr(i)): Next
    
    
    Debug.Print Join(arr, ", ")
End Sub

Private Function Fibonacci(N) As Variant
    If N <= 1 Then
        Fibonacci = N
    Else
        Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
    End If
End Function


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array




Go: package main

import "fmt"

func main() {
    for _, i := range []int{1, 2, 3, 4, 5} {
        fmt.Println(i * i)
    }
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Apply a callback to an array






PHP: function cube($n)
{
   return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
Task: Apply a callback to an array






PHP: function cube($n)
{
   return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);






Ada: with Ada.Text_Io;
 with Ada.Integer_text_IO;
 
 procedure Call_Back_Example is
    
    
   
    
    procedure Display(Location : Positive; Value : Integer) is
    begin
       Ada.Text_Io.Put("array(");
       Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
       Ada.Text_Io.Put(") = ");
       Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
       Ada.Text_Io.New_Line;
    end Display;
   
    
    type Call_Back_Access is access procedure(L : Positive; V : Integer);
   
    
    type Value_Array is array(Positive range <>) of Integer;
   
    
    procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
    begin
       for I in Values'range loop
          Worker(I, Values(I));
       end loop;
    end Map;
   
    
    Sample : Value_Array := (5,4,3,2,1);
   
 begin
    Map(Sample, Display'access);   
 end Call_Back_Example;
Task: Apply a callback to an array






PHP: function cube($n)
{
   return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. Map.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  Table-Size CONSTANT 30.

       LOCAL-STORAGE SECTION.
       01  I USAGE UNSIGNED-INT.

       LINKAGE SECTION.
       01  Table-Param.
           03  Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.

       01  Func-Id PIC X(30).

       PROCEDURE DIVISION USING Table-Param Func-Id.
           PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
               CALL Func-Id USING BY REFERENCE Table-Values (I)
           END-PERFORM

           GOBACK
           .
Task: Sum of squares



C#: using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static int SumOfSquares(IEnumerable<int> list)
    {
        return list.Sum(x => x * x);
    }
    static void Main(string[] args)
    {
        Console.WriteLine(SumOfSquares(new int[] { 4, 8, 15, 16, 23, 42 })); 
        Console.WriteLine(SumOfSquares(new int[] { 1, 2, 3, 4, 5 })); 
        Console.WriteLine(SumOfSquares(new int[] { })); 
    }
}









Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares

C: #include <stdio.h>

double squaredsum(double *l, int e)
{
   int i; double sum = 0.0;
   for(i = 0 ; i < e ; i++) sum += l[i]*l[i];
   return sum;
}

int main()
{
   double list[6] = {3.0, 1.0, 4.0, 1.0, 5.0, 9.0};
   
   printf("%lf\n", squaredsum(list, 6));
   printf("%lf\n", squaredsum(list, 0));
   
   printf("%lf\n", squaredsum(NULL, 0));
   return 0;
}











Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares


C++: #include <iostream>
#include <numeric>
#include <vector>

double add_square(double prev_sum, double new_val)
{
  return prev_sum + new_val*new_val;
}

double vec_add_squares(std::vector<double>& v)
{
  return std::accumulate(v.begin(), v.end(), 0.0, add_square);
}

int main()
{
  
  std::vector<double> v; 
  std::cout << vec_add_squares(v) << std::endl;

  
  double data[] = { 0, 1, 3, 1.5, 42, 0.1, -4 };
  v.assign(data, data+7);
  std::cout << vec_add_squares(v) << std::endl;
  return 0;
}










Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares




Go: package main

import "fmt"

var v = []float32{1, 2, .5}

func main() {
    var sum float32
    for _, x := range v {
        sum += x * x
    }
    fmt.Println(sum)
}








Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares





Java: public class SumSquares
{
 public static void main(final String[] args)
 {
  double sum = 0;
  int[] nums = {1,2,3,4,5};
  for (int i : nums)
   sum += i * i;
  System.out.println("The sum of the squares is: " + sum);
 }
}







Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares







Python: sum([1, 2, 3, 4]²)





Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares








VB: Public Sub sum_of_squares()
    Debug.Print WorksheetFunction.SumSq([{1,2,3,4,5,6,7,8,9,10}])
End Sub




Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test_Sum_Of_Squares is
   type Float_Array is array (Integer range <>) of Float;

   function Sum_Of_Squares (X : Float_Array) return Float is
      Sum : Float := 0.0;
   begin
      for I in X'Range loop
         Sum := Sum + X (I) ** 2;
      end loop;
      return Sum;
   end Sum_Of_Squares;
   
begin
   Put_Line (Float'Image (Sum_Of_Squares ((1..0 => 1.0)))); 
   Put_Line (Float'Image (Sum_Of_Squares ((3.0, 1.0, 4.0, 1.0, 5.0, 9.0))));
end Test_Sum_Of_Squares;
Task: Sum of squares

C: #include <stdio.h>

double squaredsum(double *l, int e)
{
   int i; double sum = 0.0;
   for(i = 0 ; i < e ; i++) sum += l[i]*l[i];
   return sum;
}

int main()
{
   double list[6] = {3.0, 1.0, 4.0, 1.0, 5.0, 9.0};
   
   printf("%lf\n", squaredsum(list, 6));
   printf("%lf\n", squaredsum(list, 0));
   
   printf("%lf\n", squaredsum(NULL, 0));
   return 0;
}
Task: Sum of squares



C#: using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static int SumOfSquares(IEnumerable<int> list)
    {
        return list.Sum(x => x * x);
    }
    static void Main(string[] args)
    {
        Console.WriteLine(SumOfSquares(new int[] { 4, 8, 15, 16, 23, 42 })); 
        Console.WriteLine(SumOfSquares(new int[] { 1, 2, 3, 4, 5 })); 
        Console.WriteLine(SumOfSquares(new int[] { })); 
    }
}
Task: Sum of squares


C++: #include <iostream>
#include <numeric>
#include <vector>

double add_square(double prev_sum, double new_val)
{
  return prev_sum + new_val*new_val;
}

double vec_add_squares(std::vector<double>& v)
{
  return std::accumulate(v.begin(), v.end(), 0.0, add_square);
}

int main()
{
  
  std::vector<double> v; 
  std::cout << vec_add_squares(v) << std::endl;

  
  double data[] = { 0, 1, 3, 1.5, 42, 0.1, -4 };
  v.assign(data, data+7);
  std::cout << vec_add_squares(v) << std::endl;
  return 0;
}
Task: Sum of squares





Java: public class SumSquares
{
 public static void main(final String[] args)
 {
  double sum = 0;
  int[] nums = {1,2,3,4,5};
  for (int i : nums)
   sum += i * i;
  System.out.println("The sum of the squares is: " + sum);
 }
}
Task: Sum of squares








VB: Public Sub sum_of_squares()
    Debug.Print WorksheetFunction.SumSq([{1,2,3,4,5,6,7,8,9,10}])
End Sub
Task: Sum of squares




Go: package main

import "fmt"

var v = []float32{1, 2, .5}

func main() {
    var sum float32
    for _, x := range v {
        sum += x * x
    }
    fmt.Println(sum)
}
Task: Sum of squares







Python: sum([1, 2, 3, 4]²)
Task: Sum of squares



C#: using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static int SumOfSquares(IEnumerable<int> list)
    {
        return list.Sum(x => x * x);
    }
    static void Main(string[] args)
    {
        Console.WriteLine(SumOfSquares(new int[] { 4, 8, 15, 16, 23, 42 })); 
        Console.WriteLine(SumOfSquares(new int[] { 1, 2, 3, 4, 5 })); 
        Console.WriteLine(SumOfSquares(new int[] { })); 
    }
}








Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares


C++: #include <iostream>
#include <numeric>
#include <vector>

double add_square(double prev_sum, double new_val)
{
  return prev_sum + new_val*new_val;
}

double vec_add_squares(std::vector<double>& v)
{
  return std::accumulate(v.begin(), v.end(), 0.0, add_square);
}

int main()
{
  
  std::vector<double> v; 
  std::cout << vec_add_squares(v) << std::endl;

  
  double data[] = { 0, 1, 3, 1.5, 42, 0.1, -4 };
  v.assign(data, data+7);
  std::cout << vec_add_squares(v) << std::endl;
  return 0;
}









Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares

C: #include <stdio.h>

double squaredsum(double *l, int e)
{
   int i; double sum = 0.0;
   for(i = 0 ; i < e ; i++) sum += l[i]*l[i];
   return sum;
}

int main()
{
   double list[6] = {3.0, 1.0, 4.0, 1.0, 5.0, 9.0};
   
   printf("%lf\n", squaredsum(list, 6));
   printf("%lf\n", squaredsum(list, 0));
   
   printf("%lf\n", squaredsum(NULL, 0));
   return 0;
}










Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)
Task: Sum of squares





Java: public class SumSquares
{
 public static void main(final String[] args)
 {
  double sum = 0;
  int[] nums = {1,2,3,4,5};
  for (int i : nums)
   sum += i * i;
  System.out.println("The sum of the squares is: " + sum);
 }
}






Fortran: real, dimension(1000) :: a = (/ (i, i=1, 1000) /)
real, pointer, dimension(:) :: p => a(2:1)       
real :: result, zresult

result = sum(a*a)    

result = sum(a**2)   

zresult = sum(p*p)