Sum of squares: Difference between revisions

From Rosetta Code
Content added Content deleted
(add Common Lisp example)
Line 121: Line 121:
alert( sumsq( [1,2,3,4,5] ) ); // 55
alert( sumsq( [1,2,3,4,5] ) ); // 55

{{libheader|Functional}}
Functional.reduce("x+y*y", 0, [1,2,3,4,5]) // 55


=={{header|OCaml}}==
=={{header|OCaml}}==

Revision as of 05:32, 6 March 2008

Task
Sum of squares
You are encouraged to solve this task according to the task description, using any language you may know.

Write a program to find the sum of squares of a numeric vector. The program should work on a zero-length vector (with an answer of 0).

ALGOL 68

The computation can be written as a loop.

main1:(
  PROC sum of squares = ([]REAL argv)REAL:(
    REAL sum := 0;
    FOR i FROM LWB argv TO UPB argv DO
      sum +:= argv[i]**2
    OD;
    sum
  );

  printf(($xg(0)l$,sum of squares([]REAL(3, 1, 4, 1, 5, 9))));
)

Output:

133

Another implementation could define a procedure (PROC) or operator (OP) called map.

main2:(
  []REAL data = (3, 1, 4, 1, 5, 9);

  PROC map = ( PROC(REAL)REAL func, []REAL argv)REAL:
    ( REAL out:=0; FOR i FROM LWB argv TO UPB argv DO out:=func(argv[i]) OD; out);

  REAL sum := 0;
  printf(($xg(0)l$, map ( ((REAL argv)REAL: sum +:= argv ** 2), data) ));

  PRIO MAP = 5; # the same priority as the operators <, ≤, ≥, & > maybe... #
  OP MAP = ( PROC(REAL)REAL func, []REAL argv)REAL:
    ( REAL out:=0; FOR i FROM LWB argv TO UPB argv DO out:=func(argv[i]) OD; out);

  sum := 0;
  printf(($xg(0)l$, ((REAL argv)REAL: sum +:= argv ** 2) MAP data ))
)

Output:

133
133

BASIC

Works with: QuickBasic version 4.5

Assume the numbers are in a DIM called a.

sum = 0
FOR I = LBOUND(a) TO UBOUND(a)
   sum = sum + a(I) ^ 2
NEXT I
PRINT "The sum of squares is: " + sum

Common Lisp

(defun sum-of-squares (vector)
  (loop for x across vector sum (expt x 2)))

Forth

: fsum**2 ( addr n -- f )
  0e
  dup 0= if 2drop exit then
  floats bounds do
    i f@ fdup f* f+
  1 floats +loop ;

create test 3e f, 1e f, 4e f, 1e f, 5e f, 9e f,
test 6 fsum**2 f.     \ 133.

Haskell

sumOfSquares = sum . map (^ 2)
> sumOfSquares [3,1,4,1,5,9]
133

IDL

print,total(array^2)

J

ss=: +/ @: *:

That is, sum composed with square. The verb also works on higher-ranked arrays. For example:

   ss 3 1 4 1 5 9
133
   ss $0           NB. $0 is a zero-length vector
0
   x=: 20 4 ?@$ 0  NB. a 20-by-4 table of random (0,1) numbers
   ss x
9.09516 5.19512 5.84173 6.6916

The computation can also be written as a loop. It is shown here for comparison only and is highly non-preferred compared to the version above.

ss1=: 3 : 0
 z=. 0
 for_i. i.#y do. z=. z+*:i{y end.
)

   ss1 3 1 4 1 5 9
133
   ss1 $0
0
   ss1 x
9.09516 5.19512 5.84173 6.6916

Java

Assume the numbers are in a double array called "nums".

...
double sum = 0;
for(double a : nums){
  sum+= a * a;
}
System.out.println("The sum of the squares is: " + sum);
...

JavaScript

function sumsq(array) {
  var sum = 0;
  for(var i in array)
    sum += array[i] * array[i];
  return sum;
}

alert( sumsq( [1,2,3,4,5] ) );   // 55
Library: Functional
Functional.reduce("x+y*y", 0, [1,2,3,4,5])   // 55

OCaml

List.fold_left (fun sum a -> sum + a * a) 0 ints
List.fold_left (fun sum a -> sum +. a *. a) 0. floats

Perl

The computation can be written as a loop.

sub sum_of_squares {
  $sum = 0;
  foreach (@_) {
    $sum += $_ * $_;
  }
  return $sum;
}

print sum_of_squares(3, 1, 4, 1, 5, 9), "\n";

Output:

133

Other implementation with map use.

@data = qw(3 1 4 1 5 9);
$sum = 0;
map { $sum += $_ ** 2 } @data;

print "$sum\n";

Output:

133

Scheme

(define (sum-of-squares l)
  (apply + (map * l l)))
> (sum-of-squares (list 3 1 4 1 5 9))
133