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

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'
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...
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...
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 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)
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
Write a Python program that reads in two times, an earlier time and a later time,...
Write a Python program that reads in two times, an earlier time and a later time, and prints the difference between the two times in minutes as well as in hours/minutes. The format of the time is HH:MMAM or H:MMAM or HH:MMPM or H:MMPM. Also the AM or PM may be in lower case. A sample run of the program is shown below: $ python3 Time.py Enter Earlier Time: 9:36aM Enter Later Time: 6:22PM Number of minutes between 9:36aM and...
1. Write Python code to - (a) ask user to enter a list of animals, one...
1. Write Python code to - (a) ask user to enter a list of animals, one item at a time, until “done” to terminate input (b) randomly display a selected item from the list, starting with “I like ______”. When run, the output should look something like this: Enter an item; 'done' when finished: cats Enter an item; 'done' when finished: dogs Enter an item; 'done' when finished: guinea pigs Enter an item; 'done' when finished: done You are done...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT