Submit your homework solution on Canvas (as text file, pdf, etc.).
Let $F$ be the Fibonacci numbers ($F_0 = 1, F_1 = 1, … F_{k+1} = F_k + F_{k-1}$). Prove the following: \(\sum_{i=1}^{N-2}F_i = F_N - 2\)
Prove the following formulas:
a. $\sum_{i=1}^{N}(2i-1) = N^2$
b. $\sum_{i=1}^{N}i^3 = (\sum_{i=1}^{N}i)^2$
Order the following functions by growth rate: $N, \sqrt{N}, N^{1.5}, N^2, N\log N, N \log \log N, N \log^2 N, N \log(N^2), 2/N, 2^N, 2^{N/2}, 37, N^2\log N, N^3$.
Indicate which functions grow at the same rate.
Suppose $T_1(N) = O(f(N))$ and $T_2(N) = O(f(N))$. Which of the following are true?
a. $T_1(N) + T_2(N) = O(f(N))$
b. $T_1(N) - T_2(N) = o(f(N))$
c. $\dfrac{T_1(N)}{T_2(N)} = O(1)$
d. $T_1(N) = O(T_2(N))$
Find two functions $f(N)$ and $g(N)$ such that neither $f(N) = O(g(N))$ nor $g(N) = O(f(N))$.
For each of the following six program fragments, give an analysis of the running time (Big-O will do).
(1)
sum = 0;
for( i = 0; i < n; i++ )
sum++;
(2)
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < n; j++ )
sum++;
(3)
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < i; j++ )
sum++;
(4)
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < n * n; j++ )
sum++;
(5)
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < n; j++ )
for( k = 0; k < n; k++ )
sum++;
(6)
sum = 0;
for( i = 0; i < n; i++ )
for( j = 0; j < n; j++ )
if( j % i == 0 )
for( k = 0; k < j; k++ )
sum++;
Give a precise count on the number of multiplications used by the fast exponentiation routine. (Hint: Consider the binary representation of $N$)
public static long pow( long x, int n )
{
if( n == 0 )
return 1;
if( n == 1 )
return x;
if( isEven( n ) )
return pow( x * x, n / 2 );
else
return pow( x * x, n / 2 ) * x;
}
Suppose that line 15 in the binary search routine below had the statement low = mid
instead of low = mid + 1
. Would the routine still work?
/**
* Performs the standard binary search.
* @return index where item is found, or -1 if not found.
*/
public static <AnyType extends Comparable<? super AnyType>>
int binarySearch( AnyType [ ] a, AnyType x )
{
int low = 0, high = a.length - 1;
while( low <= high )
{
int mid = ( low + high ) / 2;
if( a[ mid ].compareTo( x)<0)
low = mid + 1; /* Line 15 */
else if( a[ mid ].compareTo( x )>0)
high = mid - 1;
else
return mid; // Found
}
return NOT_FOUND; // NOT_FOUND is defined as -1
}