Big O notation for triangular numbers?

前端 未结 6 1662
春和景丽
春和景丽 2020-12-11 18:13

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_som         


        
相关标签:
6条回答
  • 2020-12-11 18:56

    O(!n) handles cases for a factorial computation (triangular time).

    It can also be represented as O(n^2) to me this seems to be a bit misleading as the amount being executed is always going to be half as much as O(n^2) would perform.

    0 讨论(0)
  • 2020-12-11 19:00

    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.

    0 讨论(0)
  • 2020-12-11 19:01

    If you think about it mathematically, the area of the triangle you are computing is ((n+1)^2)/2. This is therefore the computational time: O(((n+1)^2)/2)

    0 讨论(0)
  • 2020-12-11 19:08

    The computation time increases by the factor of N*(N + 1)/2 for this code. This is essentially O(N^2).

    0 讨论(0)
  • 2020-12-11 19:08

    when the input increases from N to 2N then running time of your algorithm will increase from t to 4t

    thus running time is proportional to the square of the input size

    so algorithm is O( n^2 )

    0 讨论(0)
  • 2020-12-11 19:09

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

    0 讨论(0)
提交回复
热议问题