(1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n.
(2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)?
(3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2).
(4) Write out the algorithm (pseudocode) to find K in the ordered array by the method that compares K to every fourth entry until K itself or an entry larger than K is found, and then, in the latter case, searches for K among the preceding three. How many comparisons does your algorithm do in the worst case?
(5) Design and implement (meaning write code and execute the code turning in test cases and source code) for the following two algorithms to raise an integer to an integer power assume in both cases that n, the exponent, is a power of 2:
Again, in case you don’t have any programming language at hand you can use pseudocode to solve the problem.
Algorithm 1
X**N = X* X**(N-1)
X**0 = 1
Algorithm 2
n = 2**m
X**n = ((X**2)**2)**2…, etc. [NOTE: the symbol of power (**) is used m times here, i.e., X**8 = ((X**2)**2)**2, because 8 = 2**3].
Which algorithm is more efficient with respect to the number of multiplications?
(6) Answer questions (a) and (b) below:
(a) How many times exactly is the code block below executed?
For (i = 1, n)
{
For (j = 1, i)
{
For (k = 1, j)
{
code block
}
}
}
Hint: You have to start with n=1, then make assumption what you make expect for any given n = N, and check if the formula you found works for n =N+1.
This is what we call prove by induction.
(b) What is the theta value of this code segment?
1)
Mathematical Expression:
Mathematical Formula:
We know that sum of all natural numbers is given by the formula : n(n+1)/2
Sum of all natural numbers from a to n = Sum of natural numbers upto n - Sum of natural numbers upto (a-1)
Hence,
Algorithm:
INPUT : Two integers, a and n
OUTPUT: An integer, sum
Step 1. Input a and n
Step 2. If a < 1 Or a > n Then
i. Print "Error in Input”
ii. Exit
(End of if condition)
Step 3. Initialize sum := 0
Step 4. Set i := a
Step 5. Repeat While i <= n
i. sum := sum + i
ii. i := i + 1
(End of while loop)
Step 6. Print sum
Code in C
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a, n, sum, i;
//Input a and n
printf("Enter any integer, n :");
scanf("%d", &n );
printf("Enter another integer, a ( 1<=a<=n ) :") ;
scanf("%d", &a );
//Test if 1 <= a <= n
if ( a < 1 || a > n )
{
printf ("Invalid value of a.... Exiting program");
exit(1);
}
else
{
//Find value of sum of all numbers from a to n and store it in variable sum
sum = 0;
i = a;
while ( i <= n)
{
sum = sum + i;
i++;
}
//Output value of sum
printf ("\nSum = %d", sum);
}
}
Output
2)
For small input sizes, Algorithm – 1 will execute faster than Algorithm – 2. However, as the value of n increases, the time required for executing Algorithm – 1 will grow faster. This will happen until a particular value of n, when f(n) = g(n). Beyond this value of n, Algorithm – 2 will execute faster.
Computation of n for which f(n) = g(n):
Hence, Algorithm – 1 will execute faster than Algorithm – 2 for all values of n which are less than or equal to 25. Beyond that, Algorithm – 2 will execute faster.
3)
The sum of squares of first n natural numbers is given by the formula :
Hence,
O (
)
Hence,
Hence
Hence the statement that
is not true.
5)
CODE IN C
#include<stdio.h>
//This function raises an integer by an integer power using algorithm 1
int algorithm1 ( int x, int n)
{
if ( n == 0 )
return 1;
return (x * algorithm1(x, n-1));
}
//This function raises an integer by an integer power using algorithm 2
int algorithm2 ( int x, int n)
{
int retval = 1, i;
for ( i = 1; i <= n ; i++ )
retval = retval * x;
return retval;
}
int main()
{
int number, exponent;
//Input numbers and exponents
printf("Enter any integer, (base) :");
scanf("%d", &number);
printf("Enter exponent :");
scanf("%d", &exponent);
//Sample run for algorithm 1
printf("Answer using algorithm 1 = %d", algorithm1(number, exponent));
//Sample run for algorithm 1
printf("\nAnswer using algorithm 2 = %d", algorithm2(number, exponent));
return 0;
}
OUTPUT
(Please note that the minimum requirement of answering the first three sub-sections has been fulfilled)
(1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...
3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...
4. [16 marks total (6 marks each)] Do a worst-case analysis for the following algorithm segments, counting the number of multiplications which occur. I have marked the lines with the multiplications you are to count with ). For all of these algorithms, use n as your fixed input size (even though n doesn't really represent the "size" of the input). Be sure to include an explanation with your answers to obtain full marks. (a) t-10; for (i-1;in-H) t-5*t; (b) (For...
a. Write a pseudocode for computing for any positive integer n Besides assignment and comparison, your algorithm may only use the four basic arithmetical operations. What is the time efficiency of your algorithm for the worst and best cases? Justify your answer. (The basic operation must be identified explicitly). Give one instance for the worst case and one instance for the best case respectively if there is any difference between the worst case and best case. Otherwise please indicate that...
Problem 1: Give the exact and asymptotic formula for the number f(n) of letters “A” printed by Algo- rithm PRINTAs below. Your solution must consist of the following steps: (a) First express f(n) using a summation notation 2 (b) Next, give a closed-form formula for f(n). (c) Finally, give the asymptotic value of the number of A's (using the O-notation.) Include justification for each step. Note: If you need any summation formulas for this problem, you are allowed to look...
(b) Use the identities above and the formula for the sum of a geometric series to prove that if n is an integer and je[1,2,..., n) then sin2 (2ntj/n) = n/2 t-1 so long as jメ1n/21, where Ir] is the greatest integer that is smaller than or equal to x. We were unable to transcribe this image
(b) Use the identities above and the formula for the sum of a geometric series to prove that if n is an integer...
Using the pseudocode answer these questions
Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 4. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a worst-case input of size n. Show your work. 5. Calculate how many times the comparison A[i]A[i+1] > A[i+2] is done for a best-case input of size n. Show your work.
DEFINITION: For a positive integer n, τ(n) is the number of
positive divisors of n and σ(n) is the sum of those divisors.
4. The goal of this problem is to prove the inequality in part (b), that o(1)+(2)+...+on) < nº for each positive integer n. The first part is a stepping-stone for that. (a) (10 points.) Fix positive integers n and k with 1 <ksn. (i) For which integers i with 1 <i<n is k a term in the...
1) Give the formula for computing Sum of k as k varies from 1 to N = 2) Give the formula for computing all possible ways of assigning 100 boxes to 5 trucks = 3) How many unique pairs are there with N objects? Give the formula.
4) [15 points total (5 points each)] Assume you are given a sorted array A of n numbers, where A is indexed from 1 up to n, anda number num which we wish to insert into A, in the proper sorted position. The function Search finds the minimum index i such that num should be inserted into Ali]. It searches the array sequentially until it finds the location i. Another function MakeRoom moves A[i], .., AIn] to Ali+1]...AIn+1] same sort...
Induction proofs. a. Prove by induction: n sum i^3 = [n^2][(n+1)^2]/4 i=1 Note: sum is intended to be the summation symbol, and ^ means what follows is an exponent b. Prove by induction: n^2 - n is even for any n >= 1 10 points 6) Given: T(1) = 2 T(N) = T(N-1) + 3, N>1 What would the value of T(10) be? 7) For the problem above, is there a formula I could use that could directly calculate T(N)?...