Question

In: Computer Science

PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...

PYTHON

Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer.

1a

int a = 0, b = 0;
for (int i = 0; i< N; i++) {
    a = a + i;
}
for (int j =
0; j < N; j++) {
    b = b + j;
}

1b

bool isPrime(int N) {
if (N == 1) return false;
for (x = 2; x <= sqrt(N); x++) {
    if (N % x == 0) {
      return false;
    }
}
return true;
}

1c

int a = 0, b = 0;
for (i = 0; i< N; i++) {
    for (j = 0; j < N; j++) {
        a += i + j;
    }
}
for (k = 0; k < N; k++) {
   b += k;
}

1d

FindMax(x, y) {
   if (x > y) {
      return x
   }
   else {
      return y
   }
}

1e

int a = 0, i = N;
while (i>0) {
    a += i;
i /= 2;
}

Solutions

Expert Solution


Related Solutions

What is the time complexity of the following code? (in big-O notaion) sum = 0 var...
What is the time complexity of the following code? (in big-O notaion) sum = 0 var = 0 product = 0 for i = 1 to (n+2){ sum = sum + 2i for j = i to [(n*n)+i]{ var = sum + var for k = sum + var{ product = var++ } } } for m + 1 to (j*i){ sum = sum + product }
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can...
1. Find the big−O, big−Ω estimate for x7y3+x5y5+x3y7. [Hint: Big-O, big- Θ, and big-Omega notation can be extended to functions in more than one variable. For example, the statement f(x, y) is O(g(x, y)) means that there exist constants C, k1, and k2 such that|f(x, y)|≤C|g(x, y)|whenever x > k1 and y > k2] 2. Find a div b and a mod b when: (a) a = 30303, b = 333 (b) a = −765432, b = 3827 3. Convert...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
Python: Devising a secret code that maps 'one' to 'two'. We send 'o' to 't', 'n'...
Python: Devising a secret code that maps 'one' to 'two'. We send 'o' to 't', 'n' to 'w' and 'e' to 'o'. Can't find any code that sends 'five' to 'ten', as the words have different length. Can't find a code that sends 'foo' to 'bar', as we would need 'o' to represent 'a' and 'r'. Likewise we cannot send 'four' to 'aaaa', as there would be no way to map the letters back. How would I write this Boolean...
I have a function and I was tild to give the big o notation of it...
I have a function and I was tild to give the big o notation of it Analyze the worst-case run-time complexity of the member function reverse() of the List. Give the complexity in the form of Big-O. Your analysis can be informal; however, it must be clearly understandable template <typename T> void List<T>::reverse() { auto current = head; // starting from head node   while(current != nullptr) // till current node is not last (next after last)   {     std::swap(current->next, current->prev); //...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
Write a Python program to add, multiply and divide any two numbers.
Write a Python program to add, multiply and divide any two numbers.
How to determine whether the following statements about big-O notation are true or false? (a) Let...
How to determine whether the following statements about big-O notation are true or false? (a) Let f(n) = √ n log n − 4, then f(n) = O(n^ 2) (b) Let f(n) = 4 n + 2 log^ 2 (n), then f(n) = O(log^ 2 (n)) (c) Let f(n) = 5 √ n + 2, then f(n) = Ω(log^ 4 (n)) (d) Let f(n) = 5 n^ 2 + 5 n log n + 4, then f(n) = O(n^3 )...
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2....
Match the following functions to their respective big-O notation 1. 5 + 0.001n3 + 0.025n 2. 100nlog n + n5 + 100n 3. n2log n + nlog n 4. 2n + n5 + 5n 5. 100n + 0.01n2 A. O(n3) B. O(5n) C. O(n2) D. O(n5) E. O(n2log n)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT