Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Big O notation for triangular numbers?

Tags:

big-o

What's the correct big O notation for an algorithm that runs in triangular time? Here's an example:

func(x):
  for i in 0..x
    for j in 0..i
      do_something(i, j)

My first instinct is O(n²), but I'm not entirely sure.

like image 416
zildjohn01 Avatar asked Jul 05 '10 12:07

zildjohn01


People also ask

How do you represent triangular numbers?

The triangular number sequence is the representation of the numbers in the form of equilateral triangle arranged in a series or sequence. These numbers are in a sequence of 1, 3, 6, 10, 15, 21, 28, 36, 45, and so on. The numbers in the triangular pattern are represented by dots.

What is the nth term for triangular numbers?

The nth triangular number in the sequence is the number of dots it would take to make an equilateral triangle with n dots on each side. The formula for the nth triangular number is (n)(n + 1) / 2.

What is the 45th triangular number?

0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666... (This sequence is included in the On-Line Encyclopedia of Integer Sequences (sequence A000217 in the OEIS).)

What's the 12th triangular number?

Checking using the method above shows that the 12th triangular number is 78.


2 Answers

Yes, N*(N+1)/2, when you drop the constants and lower-order terms, leaves you with N-squared.

like image 142
Brian Avatar answered Dec 22 '22 00:12

Brian


Yeah, O(n^2) is definitly correct. If I recall correctly, O is anyway always an upper bound, so O(n^3) should IMO also be correct, as would O(n^n) or whatever. However O(n^2) seems to be the most tight one that is easily deductable.

like image 36
Janick Bernet Avatar answered Dec 21 '22 23:12

Janick Bernet