Algorithm A 1. Set sum = 0. // 1 assignment2. Set i = 0. // 1 assignment 3. While (i <= n): // n comparisonsa.Set sum = sum + i. // n arithmetic operations, n assignmentsb.Increment i by 1. // n increments 4.Display (sum). // 1 write Algorithm B 1.Set sum = (n × (n + 1))/2. // 3 arithmetic operations, 1 assignment2.Display (sum). // 1 write Both, Algorithm A and Algorithm B, perform the same task. This means that both determine the sum of the first nnatural numbers. Algorithm A adds each number iteratively to a variable, sum. However, Algorithm B uses a formula to calculate the sum of the first n natural numbers. The execution time required for Algorithm A is given by:T = (n + 2) × a + n × b + 1 × c + n × d + n × eT = an + 2n + bn + c + dn + enT = c + n (a + b + d + e + 2)As T is a linear function of n, the algorithm is of the order, O (n).Now, determine the time required to execute Algorithm B:T = 1 × a +1 × c + 3 × eT = a + c + 3eUnlike Algorithm A, the time taken by Algorithm B is constant and does not depend on the value of n. Therefore, the algorithm is of the order __________ ?
Question
Algorithm A 1. Set sum = 0. // 1 assignment2. Set i = 0. // 1 assignment 3. While (i <= n): // n comparisonsa.Set sum = sum + i. // n arithmetic operations, n assignmentsb.Increment i by 1. // n increments 4.Display (sum). // 1 write Algorithm B 1.Set sum = (n × (n + 1))/2. // 3 arithmetic operations, 1 assignment2.Display (sum). // 1 write Both, Algorithm A and Algorithm B, perform the same task. This means that both determine the sum of the first nnatural numbers. Algorithm A adds each number iteratively to a variable, sum. However, Algorithm B uses a formula to calculate the sum of the first n natural numbers. The execution time required for Algorithm A is given by:T = (n + 2) × a + n × b + 1 × c + n × d + n × eT = an + 2n + bn + c + dn + enT = c + n (a + b + d + e + 2)As T is a linear function of n, the algorithm is of the order, O (n).Now, determine the time required to execute Algorithm B:T = 1 × a +1 × c + 3 × eT = a + c + 3eUnlike Algorithm A, the time taken by Algorithm B is constant and does not depend on the value of n. Therefore, the algorithm is of the order __________ ?
Solution
The algorithm is of the order O(1).
Similar Questions
For each of the following algorithms, indicate (i) a natural size metric for itsinputs, (ii) its basic operation, and (iii) whether the basic operation count canbe different for inputs of the same size:a. computing the average of n numbersb. computing n/n!c. finding the smallest element in a list of n numbersd. reverse display a list of n numberse. reverse a list of n numbersf. pen-and-pencil algorithm for adding two n-digit decimal integers
Compare and contrast an algorithm and a program (4 marks
John, Ram, and Joseph are comparing their finances after receiving their monthly salaries and incurring some expenditures.Your task is to create a program that, with the input of their salaries and expenditures, utilizes assignment operators (+= and -=) to compute and identify which friend has the highest remaining amount.Input format :The first line of input consists of two space-separated integers, representing John's salary and expenditure.The second line of input consists of two space-separated integers, representing Ram's salary and expenditure.The third line of input consists of two space-separated integers, representing Joseph's salary and expenditure.Output format :The output displays the name of the friend who has the highest remaining amount in the format "{Name} has more money".Refer to the sample output for the formatting specifications.Code constraints :In the given scenario, the test cases will fall under the following constraints:100 ≤ salary ≤ 1050 ≤ expenditure ≤ 105Sample test cases :Input 1 :2000 501500 307500 10Output 1 :Joseph has more moneyInput 2 :50000 1000057000 20000100000 100000Output 2 :John has more moneyInput 3 :2500 103500 251000 10Output 3 :Ram has more money
Algorithm Analysis
Consider the following recursive algorithm.ALGORITHM Q(n)//Input: A positive integer nif n = 1 return 1else return Q(n − 1) + 2 ∗ n − 1a. Set up a recurrence relation for this function’s values and solve it to deter-mine what this algorithm computes.b. Set up a recurrence relation for the number of multiplications made by thisalgorithm and solve it.c. Set up a recurrence relation for the number of additions/subtractions madeby this algorithm and solve it.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.