Compute the total running time for each of the functions in the following code. First, compute the running time in terms of some constants a, b, c, d, e, etc. Show your work and give the answer in the text box. Then give the tightest big-O bound you can for each. (All of the functions are O(n!), but you can give a more informative bound for each.)
void f(int n) {
int i = 1;
while (i <= sqrt(n)) {
if (n % i == 0 ) {
printf("%d, %d\n", i, (n / i));
}
i += 1;
}
}
int main() {
f(36);
}
In main function we are doing nothing but calling another function f().So time complexity of main function is as same as time complexity of function f(),therefore we only need to compute the time complexity of function f().
void f(int n) {
int i = 1; // Declaring and initialising integer variable i = 1 ( SUPPOSE IT TAKES (a) UNIT OF TIME )
while (i <= sqrt(n)) { // This while loop runs sqrt(n) times
if (n % i == 0 ) { // Suppose checking if() condition takes (b) UNIT OF TIME
printf("%d, %d\n", i, (n / i)); // Suppose printing statement takes (c) UNIT OF TIME
}
i += 1; // This equation is as same as (i = i + 1) of suppose adding 1 to i that is ( i + 1 ) takes (d) UNIT OF TIME and then assigning i + 1 to i ( i = i + 1) takes (e) UNIT OF TIME
}
So while loop takes (b+c+d+e)√n UNIT OF TIME
}
int main() {
f(36); // Calling Function f()
}
TIME COMPLEXITY OF FUNCTION f() :-
=> a + (b+c+d+e)√n ( COMPLEXITY IN TERM OF CONSTANT (a,b,c,d,e) ) .
TIME COMPLEXITY IN big-O NOTATION IS :-
=> O(√n ) .
Compute the total running time for each of the functions in the following code. First, compute...
Question 1 (25 pts) Find the running time complexity for the following code fragments. Express your answers using either the Big-O or Big-Θ notations, and the tightest bound possible. Justify your answers. for(int count O , i -0; i < n* n; i++) for(int i0 ; j <i; j++) count++ for(int count O , i -0; i
Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. Show your work b) void func(int n) { for (int i = 0; i < n; i = i + 10) { for (int j = 0; j < i; ++i) { System.out.println("i = " + i); System.out.println("j = " + j);
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) for(int i=n-1; i >=0; i--){ for(int k=0; k < i*n; k++){ // do something that takes O(1) time } }
4. Big-Oh and Rune time Analysis: describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. howing your work is not required (although showing work may allow some partial t in the case your answer is wrong-don't spend a lot of time showing your work.). You MUST choose your answer from the following (not given in any particular order), each of which could be re-used (could be the answer for...
/* • The following code consists of 5 parts. • Find the errors for each part and fix them. • Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// ////////////// Part A. (5 points) ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) { printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// ////////////// ...
Give a big-Oh characterization, in terms of n,of the running time for each of the following code segments (use the drop-down): - public void func1(int n) { A. @(1). for (int i = n; i > 0; i--) { System.out.println(i); B. follogn). for (int j = 0; j <i; j++) System.out.println(j); c.e(n). System.out.println("Goodbye!"); D.@(nlogn). E.e(n). F.ein). public void func2 (int n) { for (int m=1; m <= n; m++) { system.out.println (m); i = n; while (i >0){ system.out.println(i); i...
(10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...
For each C++ function below, give the tightest can asymptotic upper bound that you can determine. (a) void mochalatte(int n) { for (int i = 0: i < n: i++) { count < < "iteration;" < < i < < end1: } } (b) void nanaimobar (int n) { for (int i = 1: i < 2*n: i = 2*i) { count < < "iteration;" < < i < < end1: } } void appletart (int n) { for (int...
For each algorithm, give a reasonable big-O bound on its worst-case running time. Omit unnecessary terms and constants in your bound, for example, don't say O(2n22n 1), say O(n2). (In most cases, these aren't the best possible algorithms for each task!) Briefly explain your reasoning in each case.
A)Correct this code and explain what was corrected // C code - For Loop / Array Population // This program will populate integers into an array, and then display those integers. // Developer: Johnson, William CMIS102 // Date: Mar 4, 2020 // Global constants #define INTLIMIT 10 #include <stdio.h> // This function will handle printing my name, class/section number, and the date void printStudentData() { char fullName[19] = "William J. Johnson"; char classNumber[9] = "CMIS 102"; char sectionNumber[5] = "4020";...