-10
public long seriesLoop() {
    long answer = a;        
    for (long i = 1; i < n; i++) {          
        long delta = a;             
        for (long j = 0; j < i; j++) {
            delta *= r;             
        }           
        answer += delta;        
    }       
    return answer;  
}

public long seriesClosedForm() {
    return (long) (a * (1 - Math.pow(r, n)) / (1 - r));     
}

What is the Big-O notation for these 2 methods? Why? How do we calculate the big-O of an algorithm?

mattjegan
  • 2,724
  • 1
  • 26
  • 37
J.Pei
  • 141
  • 2
  • 11

1 Answers1

0

For the first method, it is n*n = n^2. Since there are two loops, for each loop O(n) is n since you are going through each item in an array.

Second one is a constant o(1).

  • Yes, it's my answer. But my tutor said the first one should be O(n), and the second one actually is not O(1), it is O(log n). I don't know why.. – J.Pei Jul 31 '17 at 04:35