DEV Community

Daily Challenge #225 - Square'n'Sum

dev.to staff on April 14, 2020

Implement a function squareSum so that it squares any number(s) passed into it and then adds the squares together. For example, for [1, 2, 2]: it ...
Collapse
 
vidit1999 profile image
Vidit Sarkar

I am in love with Python.

square_sum = lambda lst: sum(map(int.__mul__ , *[lst]*2))

print(square_sum([1, 2, 2])) # output -> 9
print(square_sum([1, 2, 3, 4])) # output -> 30
print(square_sum([0, 3, 5, 7])) # output -> 83
print(square_sum([2, 2, 20])) # output -> 408
print(square_sum([])) # output -> 0
Collapse
 
youssefrabeiii profile image
Youssef Rabei

Javascript:

const squareSum = numbers => numbers.map(num => num ** 2).reduce((a, b) => a + b, 0)
Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli

Trying to find a more... unconventional javascript answer :D

function squareSum(numbers)
{
  if(numbers.length > 0)
  {
    let n = numbers.pop();
    return square(n) + squareSum(numbers);
  }

  return 0;
}

let squares = [0, 1];
function square(n)
{
  let absn = Math.abs(n);
  if(typeof(squares[n]) !== 'undefined')
  {
    return squares[n];
  }

  let sqr = square(absn-1) + (absn-1) + absn;

  squares[n] = sqr;

  return sqr;
}
Collapse
 
qm3ster profile image
Mihail Malo • Edited

Sum of squares is the default example of the Rayon crate:

use rayon::prelude::*;
fn sum_of_squares(input: &[i32]) -> i32 {
    input.par_iter() // <-- just change that!
         .map(|&i| i * i)
         .sum()
}

(this runs in parallel)

Apparently people here are really into onelinerization.

let sum_of_squares = |nums| nums.iter().map(|&i| i * i).sum();

No parallelism, but at least it's shorter than Python.

Collapse
 
louy2 profile image
Yufan Lou • Edited
squareSum = let square x = x * x in getSum . foldMap (Sum . square)

Btw, you gave "tests" but the tests don't have the supposed results. I know I can hand calculate but still they are not complete test cases.

Collapse
 
swizzard profile image
sam • Edited

Point free baby!

import Data.Monoid (Sum(..))

squareSum :: (Num c, Foldable t) => t c -> c
squareSum = getSum . foldMap (Sum . (^2))

(Obviously this is probably too polymorphic, you could just limit it to [Int] -> Int)

Collapse
 
kesprit profile image
kesprit

My swift solution :

func squareSum(_ array: [Int]) -> Int {
    Int(array.reduce(into: 0.0) { $0 += pow(Double($1), 2) })
}

squareSum([1, 2, 2]) // 9
squareSum([1, 2, 3, 4]) // 30
squareSum([0, 3, 5, 7]) // 83
squareSum([2, 2, 20]) // 408
squareSum([]) // 0
Collapse
 
aminnairi profile image
Amin

Elm

I really wish flip is part of the core library...

flip : (a -> b -> c) -> (b -> a -> c)
flip f a b = f b a

squareSum : List Int -> Int
squareSum integers =
    List.foldl (flip (^) 2 >> (+)) 0 integers
Collapse
 
pavi2410 profile image
Pavitra Golchha

Python

squareSum = lambda arr: sum([n ** 2 for n in arr])
Collapse
 
savagepixie profile image
SavagePixie

Elixir

def square_sum(n), do: n |> Enum.map(&(&1 * &1)) |> Enum.sum
Collapse
 
shhdharmen profile image
Dharmen Shah

JS:

function squareSum(numbers){
  return numbers.map(a=>Math.pow(a,2)).reduce((a,b)=>a+b,0);
};
Collapse
 
pavi2410 profile image
Pavitra Golchha

Kotlin

val squareSum = { arr: List<Int> -> arr.map { it * it }.sum() }