Question
Show that log a ( x ) = c *log b ( x ) for some constant c (expressed only in terms of the constants
-
-
Show that loga(x) =c*logb(x) for some constant c (expressed only in terms of the constants a and b). Hint: This is probably easier than you think... It follows quite directly from the log properties. You should, though, prove that its true for any a and b and not prove by an example.
-
-
-
Rank the following three functions: log N, log(N2), log2N. Explain.
-
-
-
Find a big-O estimate for the running time (in terms of n) of the following function (with explanation)
int mysterySum( int n ) { int i, j, s=0; for(i=0; i < n; i++) { for(j=0; j < i; j++) { s += i*i; } } }
-
Is this version of mysterySum faster? Is the big-O analysis different?
int mysterySum1( int n ) { int i, j, s=0; for(i=0; i < n; i++) { int i2 = i*i; for(j=0; j < i; j++) { s += i2; } } }
-
Replace the inner loop in mysterySum by an O(1) expression and compute the running time of the new program.
-
Find a single O(1) expression giving the same result (A mathematical expression, not code). Hint: Evaluate the function by hand (or compile and run the code) for a few values of n and try to see the pattern.
Notice: You have to find a mathematical formula, not a piece of code. Start with the expression you derived in part c above (hint: It is a series) and find the sum of the series any way you want (including looking it up).
-
-
The following program computes 2n:
int power2(int n) { if (n==0) return 1; return power2(n-1)+power2(n-1); }
-
Find a recurrence formula as we learned in class. Find the runtime. What is the big problem with this function? (hint: We discussed something similar in class).
- Introduce a small modification that makes the function run in linear time. Show why the runtime is linear.
-
-
Designing a Java class. A combination lock has the following basic properties: the combination (a sequence of three numbers) is hidden; the lock can be opened by providing the combination; and the combination can be changed, but only by someone who knows the current combination. Design a class with public methods open and changeCombo and private data fields that store the combination. The combination should be set in the constructor. Do not compile and run your code, just show it in your homework paper.
-
Review Java OO principles, specifically the idea that all Java classes are subclasses of the Object class, and thus must have all the methods of the Object class. You can see the Object class API (and any other standard class) by following the link "JDK API" on the class web page under Resources, or at the Piazza site. Choose Object in the pane titled All Classes, and the API will be displayed on the right.
- What are the three most important Object methods? (Hint: their names start with e, h, and t). Because all objects have these methods, we can use HashSet to contain any set of objects (we'll stick to sets of same-type objects in our work).
- Show equals in use by writing one if () that checks whether s String s is equal to a String t, character by character. Explain what this comparison is doing if s = abc and t = abx. Compare this to what happens in if (s == t).
- Find the hashCode and toString values for String s = abc. Also for the Integer of value 6.
-
Interfaces
- What is a Java interface?
- What members may be in an interface?
- Write a Java interface source file UnionFind.java to express the API on page 219, the union-find interface. Also rewrite just the first line of UF.java to assert that class UF implements this interface.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started