15
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ This challenge needs a winning criterion (e.g. code golf) \$\endgroup\$ – dylnan Nov 4 '18 at 19:25
  • 2
    \$\begingroup\$ This is a subset of this question \$\endgroup\$ – caird coinheringaahing Nov 4 '18 at 19:45
  • 1
    \$\begingroup\$ Can the sequence be 0 indexed? i.e. the natural numbers up to n? \$\endgroup\$ – Jo King Nov 4 '18 at 23:49
  • 5
    \$\begingroup\$ Note that it's discouraged to post challenges directly taken from somewhere else. \$\endgroup\$ – user202729 Nov 5 '18 at 5:53
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – Jonathan Allan Nov 5 '18 at 8:33

39 Answers 39

1
\$\begingroup\$

PowerShell, 73 39 bytes

1.."$args"|%{$r+=$_;$s+=$_*$_}
$r*$r-$s

Try it online!

-34 bytes thanks to @mazzy and his genius PowerShell-foo

\$\endgroup\$
  • 1
    \$\begingroup\$ nice try. iex is a power tool in the Powershell. but it is often longer than the plus operator :) Try it online! \$\endgroup\$ – mazzy May 13 at 16:39
  • \$\begingroup\$ Damn @mazzy ... Thanks for the pro-tip! \$\endgroup\$ – KGlasier May 13 at 16:42
  • \$\begingroup\$ formula as other solutions in this topic \$\endgroup\$ – mazzy May 14 at 7:48
0
\$\begingroup\$

C#, 89 Bytes

int x=0,y=0;for(int i=1;i<=Int32.Parse(s[0]);i++){x+=i*i;y+=i;}Console.Write($"{y*y-x}");

ungolfed:

int x=0,y=0;
for(int i=1;i<=Int32.Parse(s[0]);i++){
x+=i*i;
y+=i;
}
Console.Write($"{y*y-x}");

Try it online!

\$\endgroup\$
  • \$\begingroup\$ You can save 31 bytes if you use a function to read in and return ints, as well as make some changes with the for loop call. Try it online! \$\endgroup\$ – Meerkat Nov 5 '18 at 19:27
  • \$\begingroup\$ @Meerkat Is one allowed to put the Output code in the footer? Then I get to 57 bytes \$\endgroup\$ – user51497 Nov 5 '18 at 21:01
  • \$\begingroup\$ Output is supposed to be in the body. With the function call, this is what the return essentially does, which is why it's included in the body. \$\endgroup\$ – Meerkat Nov 5 '18 at 21:28
0
\$\begingroup\$

Axiom, 39 bytes

f(n)==reduce(+,[x^3-x^2 for x in 1..n])

test:

-> [[x,f x]for x in [1,2,3,10]]
     [[1,0],[2,4],[3,22],[10,2640]]
\$\endgroup\$
0
\$\begingroup\$

Clojure, 91 bytes

(fn s[n](let[u #(apply + %)q #(Math/pow % 2)m(range 1(inc n))](-(q(u m))(u(map #(q %)m)))))

The naive, literal approach. See below:

(defn sum-sq-diff [n]
    (let [; Shortcut functions to save bytes
          sum #(apply + %)
          square #(Math/pow % 2)
          nums (range 1 (inc n))

          ss1 (sum (map #(square %) nums))
          ss2 (square (sum nums))]

      (- ss2 ss1)))

(mapv sum-sq-diff [1 2 3 10 24 100])
=> [0.0 4.0 22.0 2640.0 85100.0 2.516415E7]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 24 bytes

->n{(n+n+3*n*=n)*~-n/12}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 72 bytes

x=[i+1for i in range(int(input()))]
print(sum(x)**2-sum(i**2for i in x))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

F# (Mono), 57 41 bytes

let f x=Seq.sumBy(fun y->y*y*(y-1))[1..x]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

PHP, 37 bytes

while($i<$argn)$s+=$i++*$i*$i;echo$s;

Try it online!

Standalone program input number via STDIN. Example:

$ echo 10|php -nF ssd.php
2640
$ echo 100|php -nF ssd.php
25164150
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 22 bytes

{($_³-$_)*($_/4+⅙)}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.