# Print the sum of the squares of some numbers

This is a little codegolf challenge from codingame.com. I was wondering how much Python3 code is necessary (but of course answers are not limited to Python3).

Input
The first line contains one positive integer n. The second line contains n positive integers. Each consecutive integers on the second line are separated by one space. There is no newline at the end of the second line.

Output
One line containing the sum of the squares of the numbers on the second input line, and no white space except an optional newline at the end.

Each entry must be a complete program with input and output, not only a function. Shortest code wins.

For example, input:

5
1 2 5 7 8


and output:

143

• Can we take only one input? The first one is unnecessary – Luis Mendo Mar 29 '16 at 16:10
• You do know you're changing IO allowed by default? meta.codegolf.stackexchange.com/questions/2447/… – Blue Mar 29 '16 at 16:11
• Can we take input as a list of integers? (i.e [1,2,5,7,8]) – a spaghetto Mar 29 '16 at 16:19
• I'm confused why you've posted both here and in the Sandbox ... meta.codegolf.stackexchange.com/a/8827/42963 – AdmBorkBork Mar 29 '16 at 16:20
• We've discussed the situation among the moderators and figured that the copyright is indeed probably not worth worrying about. However, the downvotes on your challenge are likely due to the fact that you've copied the input format verbatim from the source, and cumbersome I/O formats are one of the things to avoid when writing challenges. If you relaxed the rules a little, this challenge would probably blend in more nicely and be more well-received. Sorry about the premature deletion, and welcome to Programming Puzzles & Code Golf! – Dennis Mar 29 '16 at 20:37

# 05AB1E, 5 bytes

Code:

²ð¡nO


Explanation:

²      # Take the second input, the first input is ignored
ð¡    # Split on spaces
n   # Square each element
O  # Take the sum and implicitly output it


Uses CP-1252 encoding.

Try it online!

## Python 3, 52

i=input;print(sum(int(d)**2for d in i(i()).split()))

• It is giving an unnecessary output. But that sound fair since there is an unnecessary input! – aliqandil Mar 29 '16 at 17:08
• Yup, going by the rules... – shooqie Mar 29 '16 at 17:14

# Jelly, 5 bytes

ṣ⁶V²S


Try it online.

Input format is rather annoying....

## Explanation

ṣ⁶V²S
ṣ⁶       split by space
V      map eval over the resulting list -- the newline in the first element causes the first line to be treated as a separate link and ignored
²     map square over the list
S    sum

• You can save three bytes by eliminating the first three bytes. – Dennis Mar 29 '16 at 20:20
• @Dennis Oh, that's clever. Thanks. – a spaghetto Mar 29 '16 at 21:17
• In case it isn't clear, V executes the string just as if it were a complete program. For the first string, the second line is the main link, and the first one isn't referenced, so it is ignored. – Dennis Mar 29 '16 at 23:04

## Minkolang 0.15, 9 bytes

n[n2;+]N.


Try it here!

### Explanation

n            Take number from input
[    ]      Pop top of stack and repeat that many times
n          Take number from input
2;        Square it
+       Add to top of stack
N.    Output as number and stop.


## CJam, 10 bytes

l;l~]2f#1b


Test it here.

### Explanation

l;   e# Read first line and discard.
l~   e# Read second line and evaluate, dumping all integers on the stack.
]    e# Wrap them in an array.
2f#  e# Square each.
1b   e# Sum.

• 1b is a nice trick – Luis Mendo Mar 29 '16 at 16:30
• @DonMuesli I could probably use :+ here, but 1b became a habit because it also works as expected for empty arrays. – Martin Ender Mar 29 '16 at 16:31

# Python 3, 54 bytes

-[] thanks to shooqie

print(sum(int(i)**2for i in input(input()).split()))


Takes input as specified in the first version of the question.

• first version :-D – Luis Mendo Mar 29 '16 at 16:16
• Stupid me, testing in a REPL... – CalculatorFeline Mar 29 '16 at 16:28
• You don't need the square brackets: print(sum(int(i)**2for i in input(input()).split())) – shooqie Mar 29 '16 at 16:32
• @CatsAreFluffy and shooqie - Thank you, I was doing it in 56 bytes and was wondering what parts of the code are unnecessary. As opposed to my code, input(input()) saves one byte, 2for another and the square brackets another two. – mIllIbyte Mar 29 '16 at 16:43

# MATL, 6 bytes

xjU2^s


Try it online!

x    % take first input and throw it away
j    % take second input as a string
U    % convert to array with those numbers
2^   % element-wise square
s    % sum of array


# R, 22 bytes

Nothing very flash, handles the input as specified

cat(sum(scan()[-1]^2))


Test run

> cat(sum(scan()[-1]^2))
1: 5
2: 1 2 5 7 8
7:
143
>


## Pyke, 10 bytes

;\ cFE2^)s


Takes input as requested

Try it here (Yay setup a try-it page!)

Or 5 bytes if allowed a list of ints:

2Rm^s


Or noncompeting (add squared node), 3 bytes

mXs


## Lua, 74 62 Bytes

Edit: saved 8 bytes due to @KennyLau

It's quite straigthforward, skip the first line and iterate over the group of digits in the second.

i,x=io.read,0i()i():gsub("%d+",function(d)x=x+d*d end)print(x)


### Ungolfed

x=0
i()
i():gsub("%d+",function(d)
x=x+d*d
end)
print(x)

• Actually, lua can convert from string to number automatically, so you don't have to do it yourself :) – Leaky Nun Mar 30 '16 at 10:20
• Also, this actually takes up less bytes than this: x=0i=1print(x..i) x,i=0,1print(x..i) – Leaky Nun Mar 30 '16 at 10:25

# Octave, 45 bytes

Octave is not exactly designed for accepting random input formats. For instance, input over multiple lines is impossible. Also, numbers can't be entered as a list unless you have brackets. For that reason, the input must be taken as a string.

input('');disp((x=str2num(input('','s')))*x')

input('')                                     % Take the first input line and discard
input('','s')        % Take the second input as a string
x=str2num(input('','s'))       % Convert the string to a list of numbers, x
(x=str2num(input('','s')))*x'  % Multiply x by x transposed
% This is equivalent to sum(x.^2) but shorter
disp((x=str2num(input('','s')))*x') % Display the result


Call it like this:

input('');disp((x=str2num(input('','s')))*x')
5
1 2 5 7 8
143


# C,65 bytes

j,k;main(i){for(;scanf("%d",&i)+1;j++&&(k+=i*i));printf("%d",k);}


# Pyth, 10 bytes

sm^sd2ce.z


Try it here!

Takes input in the exact same format as specified in the challenge.

### Explanation

sm^sd2ce.z  # .z = input lines

e.z  # only take the last line
c     # split on spaces
m          # map each number d
sd       # convert to integer
^  2      # square
s           # sum all squares


### Alterntive solutions with more generous input formats

8 bytes if I may omit the number count:

sm^sd2cz


Try it here!

4 bytes if I may take the input as list of integers:

s^R2


Try it here!

## Pyth, 9 bytes

s^R2re.z7


Try it here

Or 7 bytes if allowed to omit line 1

s^R2rz7

• You're missing the first line of input. – isaacg Mar 29 '16 at 16:32

main=interact\$show.sum.map((^2).read).tail.words


How it works:

interact                 read whole input, pass it to a function and
print it's return value. The function is:

words     split at whitespace into words
tail          drop first element (the number on line #1)
map(       )         convert each word
(^2)                 and square
sum                    sum all values
show                     convert back to string


# GS2, 5 bytes

W",Φd


Try it online!

### How it works

W      Find all numbers in the input; push a list.
,    Square.
Φ   Map the last instruction over the list of numbers.
d  Compute the sum of all squares.


# J, 4 bytes + 100

+/*:

   +/*:1 2 5 7 8
143


A +100 punishment in the byte-count to myself for not having standard input.

• Add proper input commands please. For some reason this question is very specific about I/O formats, and even normally you can't assume input is in a variable. (And it might be shorter anyway (if you have a 1 or 2 byte input command)) – CalculatorFeline Mar 29 '16 at 20:04
• Thanks for your comment. Does J really have an input command? – Leaky Nun Mar 30 '16 at 5:15
• Yes, J has I/O. – Dennis Mar 30 '16 at 6:09

# Japt, 8 bytes

Ns1 mp x


Try it in the Japt Interpreter.

### How it works

The code is parsed as

N.s(1).m(p).x()

• N is a variable; it contains the parsed input.
• s(1) discards the first array element.
• m(p) maps p (square) over the remaining array.
• x() computes the sum of the array of squares.

# Pylongolf2, 8 bytes

c| n²+~


c  - read the input
|  - split by space (note the 1 space after |)
n  - convert all of that in the list to numbers
²  - Square everything in the list
+  - Sum everything in the list
~  - Print it


## Batch, 76 bytes

@set/px=
@set/px=
@set t=0
@for %%n in (%x%)do set/at+=%%n*%%n
@echo %t%