Sum of Squares without multiplication

5

I want to sum the squares for a given number N from 1 without using Multiplication. Is it possible?

user2212

Posted 2012-09-05T09:36:43.940

Reputation: 75

Please check the documentation properly.Try n = 10;Sum[i^2, {i, 1, n}] or Total@Table[i^2, {i, 1, n}]. – PlatoManiac – 2012-09-05T09:38:09.567

3n = 10; Sum[ Sum[j, {j, i}], {i, n}] – Rolf Mertig – 2012-09-05T09:53:34.440

Or a nested For loop.. – PlatoManiac – 2012-09-05T09:57:10.190

11HINT: $$\begin{array}{ccccc} \blacksquare & {\color{red}\blacksquare} & {\color{green}\blacksquare} & {\color{purple}\blacksquare}\ {\color{red}\blacksquare} & {\color{red}\blacksquare} & {\color{green}\blacksquare} & {\color{purple}\blacksquare}\ {\color{green}\blacksquare} & {\color{green}\blacksquare} & {\color{green}\blacksquare} & {\color{purple}\blacksquare}\ {\color{purple}\blacksquare} & {\color{purple}\blacksquare} & {\color{purple}\blacksquare} & {\color{purple}\blacksquare} \end{array}$$ – J. M.'s ennui – 2012-09-05T10:31:27.817

@DavidCarraher sure. I was too quick. n = 10; Sum[Sum[i, {i}], {i, n}] is what I meant. BTW: doing Sum[Sum[i, {i}], {i, m}] gives (1/6)(-1 + m)m(1 + m), while Sum[i^2, {i, m}] gives the correct (1/6)m(1 + m)(1 + 2*m). Not sure if this is a bug or just stretching Sum too much ... – Rolf Mertig – 2012-09-05T18:29:34.217

@Hmm, I just realized that Sum[Sum[i,{i}] == Binomial[i, 2]. Is that really what you wanted to see from the first Sum? – DavidC – 2012-09-05T19:14:12.560

The answer to "Is it possible?" is obviously yes. Because multiplication of (positive) integers is just repeated addition. – murray – 2013-05-26T15:28:49.183

Answers

9

Someone certainly had to write this recursive one:

Clear[f]
f[x_Integer, y_Integer] := x + f[x, y - 1]
f[x_Integer, 0] := 0
f[x_Integer] := f[x, x] + f[x - 1]
f[0] := 0

rm -rf

Posted 2012-09-05T09:36:43.940

Reputation: 85 395

2+1 This is in the spirit of the modern foundations of arithmetic. – whuber – 2012-09-05T19:41:51.597

8

Try this :

s[n_] := Total[ Range[n]^2]

to check how it works, e.g. :

s[5] // Trace    

There is also a purely symbolic approach, e.g. : $\quad n^2$ ~ Sum ~$n\quad$ (see Infix notation) :

(n^2) ~ Sum ~ n
1/6 (-1 + n) n (-1 + 2 n)

Note : Sum[ n^2, n] returns the same as Sum[ i^2, {i, n-1}] does, i.e. indefinite sums starts at 0 while definite ones at 1, e.g. :

Table[ Sum[ n^2,     n ], {n, 5}]
Table[ Sum[ k^2, {k, n}], {n, 5}]
{0, 1, 5, 14, 30}
{1, 5, 14, 30, 55}

Test

We added belisarius's approach (HarmonicNumber) for comparison.

st = {  Sum[i^2, {i, 10^6}]           // AbsoluteTiming,
        s[10^6]                       // AbsoluteTiming,
       (Sum[n^2, n] /. n -> 10^6 + 1) // AbsoluteTiming,
        HarmonicNumber[ 10^6, -2]     // AbsoluteTiming  };

Last @ First @ st
Equal @@ Last /@ st
333333833333500000
True
First /@ st
{1.4570000, 1.0540000, 0., 0.}

Conclusion

Symbolic computations are especially recommended

Artes

Posted 2012-09-05T09:36:43.940

Reputation: 51 831

7

Figuring out what the following snippet does is left as an exercise for the reader:

With[{n = 9},
 s = t = 0; j = 1;
 Do[
  t += j; s += t; j += 2,
  {n}]; s
 ]

J. M.'s ennui

Posted 2012-09-05T09:36:43.940

Reputation: 115 520

7I feel like browsing the wrong site ... – Dr. belisarius – 2012-09-05T11:50:07.750

7

A Mathematica minded answer:

HarmonicNumber[n, -2]

So:

Simplify[Sum[i^2, {i, n}] == HarmonicNumber[n, -2]]
(* True *)

Dr. belisarius

Posted 2012-09-05T09:36:43.940

Reputation: 112 848

Out of curiosity, how is HarmonicNumber implemented internally? You appear to presume its implementation does not involve multiplication. It's hard to see how that would be. – whuber – 2012-09-05T19:41:04.920

@whuber I doubt any Mma code is completely exempt from multiplications if you go down till machine code! Someone has to do pointer arithmetic ... – Dr. belisarius – 2012-09-05T20:20:25.010

1A lot of pointer arithmetic is done by adding. It is plausible that either at the p-code level or the machine level, many of the solutions offered here use no multiplication (nor anything even more complicated, like exponentiation). That's not so plausible for Harmonic number calculations :-). – whuber – 2012-09-05T21:39:07.007

1

@whuber You misunderstood me. There is no way to prevent multiplication. "Modern" machine addressing modes involve base + (index * scale) + displacement at the hardware level. See for example http://staff.ustc.edu.cn/~xlanchen/cailiao/x86%20Assembly%20Programming.htm or any reference to the lea instruction in assembly

– Dr. belisarius – 2012-09-05T23:13:29.743

@whuber: from what I've seen internally, HarmonicNumber is converted to PolyGamma[]; numerically evaluating that would definitely require not a few multiplications. – J. M.'s ennui – 2012-09-06T00:23:35.140

@J.M. http://i.stack.imgur.com/yj4qd.png

– Dr. belisarius – 2012-09-06T00:36:27.307

7

Total@Flatten[ConstantArray[#, #] & /@ Range[9]]

I think this exercise is somewhat of a Rorschach test… I don't know what the above says about me, though :)

F'x

Posted 2012-09-05T09:36:43.940

Reputation: 10 517

What do you think? – Dr. belisarius – 2012-09-05T17:53:54.883

1@Verde what is this, self-Rorschach? I don't think that works… “I think it says I'm handsome” – F'x – 2012-09-05T18:09:21.647

2“My shirt, boy! What do you see!?” – Dr. belisarius – 2012-09-05T18:42:43.937

5

Ten ways to beat a dead horse

Sunday afternoon on an airplane without wifi and this was the problem I remembered reading at breakfast. Forgive me for the time I had on my hands. All because @Aky resurrected this dead horse. (Thanks by the way. I'm glad to have figured out the CellularAutomaton one, but we landed before I could come up with a good MapAll application.)

No speed demons here, but I assiduously avoided Times, sometimes Plus, too (notwithstanding objections that internally some arithmetic has to be occurring).

Simple:

ss1[n_] := Range[n] + Range[n] - 1 // Accumulate // Total

ss2[n_] := Total[# ~Table~ {#} & /@ Range[n], 2]

CellularAutomaton

ss3[n_] := 
 Total[CellularAutomaton[
    {62, {2, {{{0, 0, 0}, {1, 1, 0}, {0, 0, 0}},
              {{0, 1, 0}, {1, 1, 0}, {0, 0, 0}},
              {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}}}, {1, 1, 1}}, 
   SparseArray[{{1, 1, 1} -> 1}, {n, n, n}], {{{n - 1}}}], 3]

Rules:

Clear[live, dead];
ss4[n_] := Length[Nest[Join[{{live, live}}, # /.
     {{live, live} -> Sequence[{live, live}, {live, dead}, {live, dead}, {dead, dead}],
      {live, dead} -> Sequence[{live, dead}, {dead, dead}]}] &, {}, n]]

My own SparseArray version:

ss5[n_] := SparseArray[{{i_, j_, k_} /; i <= k && j <= k -> 1},
             {n, n, n}]["NonzeroPositions"] // Length

Why not LeafCount?

ss6[n_] := Flatten[Table[FoldList[{#2, #1} &, 0, Range @ i], {i, n - 1}], 1] // LeafCount

Functional to illiterate -- I mean, illegible:

ss7[n_] := Total[{{#, #} &[Range /@ #], {{-#}}} & @ Range[n], 4]

ss8[n_] := NestList[Most, #, Length@#] & @ NestList[{0, 0, #1} &, 0, n - 1] //
            Flatten // Length

ss9[n_] := Fold[{#1} /. {0} -> #2 &, 0, #] & @
            FoldList[{#1} /. 0 -> #2 &, 0, NestList[{{#}} &, 0, n - 1]] /. {0} -> 0 // Depth

ss10[n_] := (x \[Function] #) /@ # & /@ (r \[Function] r[[1 ;; #]] & /@ r) @ Range @ n //
            Flatten // Length

Postscript

Got a MapAll one:

ssPS[n_] := Total @ Cases[
   MapAll[{Depth[#], #} &, #] & /@ NestList[{#} &, x, n - 1], _Integer, Infinity]

Michael E2

Posted 2012-09-05T09:36:43.940

Reputation: 190 928

Actually, the thread was at the top of the list of questions and (failing to notice the date) I assumed it was a new question that had received a flurry of replies. I suppose now that was because someone had edited it. +1 for the variety. – Aky – 2013-05-26T23:51:09.753

@Aky Yeah, I didn't notice the date either. And I didn't notice someone had edited it. +1 to you, too. – Michael E2 – 2013-05-26T23:53:30.437

I would also like to assure all readers that no horses were harmed during the writing of these answers. – Aky – 2013-05-26T23:55:58.457

2Heh, another silly one: With[{m = 10}, Flatten@ConstantArray[, {m, 1 + m, 1 + m + m}] ~Partition~ 6 // Dimensions // First ] (looks best on a mac) – rm -rf – 2013-05-27T00:35:04.157

1@rm-rf Yeah, , right back at you, buddy! – Michael E2 – 2013-05-27T04:43:33.480

4

Yes, it is possible.

Since multiplication of positive integers is repeated addition, we can repeatedly add instead of multiply:

n = 10;
sum = 0;
Do[
  Do[
    sum = sum + i,
    {j, 1, i}],
  {i, 1, n}];
Print[sum]

Mike Z.

Posted 2012-09-05T09:36:43.940

Reputation: 541

4

An oddball one using a recursive, memoizing function for the square.

Clear[sq]; 
sq[n_Integer] := sq[n] = sq[n - 1] + n + (n - 1) 
sq[1] = 1;

Sum[sq[n], {n, 6}]

91

It's not something I would directly use for such a goal, but you asked for something without explicit multiplications and you got it.

Alternatively, if we don't interpret Dot as some kind of multiplication then

#.# &@Range[6]

would fit the requirements too.

Sjoerd C. de Vries

Posted 2012-09-05T09:36:43.940

Reputation: 63 549

3

If exponentiation is allowed, how about this?

w = 10;
E^(Log[1/6] + Log[w] + Log[1 + w] + Log[1 + w + w])

385

Explanation

ClearAll[w]
Sum[i^2, {i, 1, w}]

1/6 w (1 + w) (1 + 2 w)

DavidC

Posted 2012-09-05T09:36:43.940

Reputation: 15 949

3

You can sum the diagonal. However, that might be slowest procedure of all. Times Table

Fred Kline

Posted 2012-09-05T09:36:43.940

Reputation: 2 226

How about Tr[t], assuming your table is t? That's what R.M. did. – DavidC – 2012-09-05T19:18:37.047

2@DavidCarraher, this is for others like me: When I look at the figures, I can feel the wheels turning. When I read the math, I find out the hamsters are dead. – Fred Kline – 2012-09-05T21:21:55.950

3Don't give up. My hamsters died a long time ago. – DavidC – 2012-09-05T23:04:01.923

3

I'll join the bandwagon with SparseArray:

sq[n_Integer] := Tr@SparseArray[{{i_, i_} :> i^2}, n]

rm -rf

Posted 2012-09-05T09:36:43.940

Reputation: 85 395

Squaring involves multiplication, doesn't it? Tsk, tsk :-). – whuber – 2012-09-05T19:39:35.110

@whuber sshhh... it was supposed to be hidden :D Ok, you could do {i_, i_} :> f[i, i], where f is from my other answer :)

– rm -rf – 2012-09-05T19:47:49.287

Original +1 but slow sq[10^6]; // AbsoluteTiming yields {2.1980000, Null}. – Artes – 2012-09-05T20:04:01.380

1@Artes Heh, of course it is slow! I thought people were generally just having fun here :) No one would really sum squares using Tr@SparseArray... (at least, I hope they don't and this isn't used in any decent code :P) – rm -rf – 2012-09-05T20:42:42.027

2

A Wolfram Alpha minded answer:

WolframAlpha["find the sequence 1,5,14,30,55"]

Rolf Mertig

Posted 2012-09-05T09:36:43.940

Reputation: 16 237

1

sos[n_Integer] := Total[Total[Range[1, #, 2]] & /@ (2 Range[n])]

Aky

Posted 2012-09-05T09:36:43.940

Reputation: 2 547

2No need to call Total[] twice: Total[Range[1, #, 2] & /@ (2 Range[n]), 2] – J. M.'s ennui – 2013-05-26T13:55:09.680

@J.M. Thank you. – Aky – 2013-05-26T14:03:56.100

0

Late to the party!

f[n_]:=Total[Accumulate[Range[1, n + n, 2]]]

So for instance the first seven are

f/@Range[7]

{1, 5, 14, 30, 55, 91, 140}

bill s

Posted 2012-09-05T09:36:43.940

Reputation: 62 963