15
1
The sum of the squares of the first ten natural numbers is, \$1^2 + 2^2 + \dots + 10^2 = 385\$
The square of the sum of the first ten natural numbers is,
\$(1 + 2 + ... + 10)^2 = 55^2 = 3025\$
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is
\$3025 − 385 = 2640\$
For a given input n, find the difference between the sum of the squares of the first n natural numbers and the square of the sum.
Test cases
1 => 0
2 => 4
3 => 22
10 => 2640
24 => 85100
100 => 25164150
This challenge was first announced at Project Euler #6.
Winning Criteria
There are no rules about what should be the behavior with negative or zero input.
The shortest answer wins.
A052149 – Shaggy – 2018-11-04T19:18:15.733
4This challenge needs a winning criterion (e.g. code golf) – dylnan – 2018-11-04T19:25:42.173
2
This is a subset of this question
– caird coinheringaahing – 2018-11-04T19:45:48.3801Can the sequence be 0 indexed? i.e. the natural numbers up to
n? – Jo King – 2018-11-04T23:49:19.8205
Note that it's discouraged to post challenges directly taken from somewhere else.
– user202729 – 2018-11-05T05:53:45.5303@Enigma I really don't think that this is a duplicate of the target since many answers here don't port easily to be answers of that, so this adds something. – Jonathan Allan – 2018-11-05T08:33:21.943
#APL(NARS), 13 chars, 26 bytes
use the formula Sumw=1..n test:
@JonathanAllan: You need to spell my name correctly for me to receive a ping ;) Correct me if I'm wrong, but isn't this just the other challenge with a hardcoded to 1? In my opinion that is close enough to warrant a dupe, but if I'm alone with that opinion, feel free to vote to reopen. – Emigna – 2018-11-05T11:34:16.703
@Emigna wow, donnu how I manegad ttah, sorry :) porting an answer from there to here is easy, but probably not the golfiest, porting back seems non-trivial for the golfiest of solutions here; therefore I feel like this, while the same topic, justifies a separate question. (BTW I cannot "vote" to reopen, just hammer :( - I'll wait for other opinions) – Jonathan Allan – 2018-11-05T13:13:41.403
1@Emigna That makes sense, but in this case I think the challenge is simple enough that a small difference makes a big difference. The answers here are all about finding the most concise formula for the answer; almost none of the answers to the other question work that way. – Misha Lavrov – 2018-11-05T14:44:24.017
very closely related: https://codegolf.stackexchange.com/questions/83338
– Titus – 2018-11-06T06:15:18.863