Anti-Triangle

Computer Science Level pending

Chris wants a list of numbers up to N N that is not divisible by any triangular numbers greater than 1. Here is his algorithm in Python:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
def anti_tri(N):
    # L[i] = 1 implies i is not divisible by any triangular number > 1
    L = [0] + [1] * N

    T = 3   # triangular number
    i = 3
    while T <= N:
        for i in range(T,N,T):
            L[i] = 0
        T += i
        i += 1

    ans = []
    for i in range(N+1):
        if L[i] == 1:
            ans.append(i)

    return ans

What is the time complexity of the algorithm above?

Details and Assumptions

  • N N : range of numbers
  • M M : number of triangular numbers smaller than N N
  • Triangular Numbers are numbers that can be expressed in the form n ( n + 1 ) / 2 n(n+1) / 2 . The first 5 triangular numbers are 1, 3, 6, 10 and 15.
O ( N 2 ) O(N^2) O ( N lg M ) O(N\lg M) O ( N ) O(N) O ( N lg 2 N ) O(N\lg ^2 N) O ( N lg N ) O(N\lg N) O ( N M ) O(NM) O ( N lg N lg M ) O(N\lg N\lg M) O ( N lg 2 M ) O(N\lg^2 M)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

0 solutions

No explanations have been posted yet. Check back later!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...