r/learnjava • • Jan 18 '20

Why is the big o n^(2)

Sum of numbers from 1 +2 + 3... N

1 Upvotes

5 comments sorted by

1

u/Philboyd_Studge Jan 18 '20

It can be done in O(1) if you're clever ;)

1

u/desrtfx Jan 18 '20

Yes, definitely in O(1). Not even that difficult.

0

u/[deleted] Jan 18 '20

[deleted]

1

u/[deleted] Jan 18 '20 edited Jan 18 '20

my teacher said its n2 and im not understanding

she said its (1+2+3... +N) * N

and thats why its n2

0

u/desrtfx Jan 18 '20

The formula you wrote in this comment is not the sum of the natural numbers and even with the * N it is still O(n).

A single multiplication does not change the algorithmic complexity.

1

u/[deleted] Jan 18 '20

It was the sum thing. That's why I don't understand how it's n2