Question

In: Computer Science

12. The reciprocal search problem is: input: a list L of n floating point numbers output:...

12. The reciprocal search problem is:

input: a list L of n floating point numbers
output: two elements a, b L such that a*b=1, or None if no such a, b exist

Design an algorithm for this problem using the greedy pattern. Write pseudocode for your algorithm below. If you write multiple drafts, circle your final draft. Hint: I expect your algorithm to be relatively simple and to have time complexity O(n2).

13. Perform a chronological step count to derive the time complexity T(n) of your algorithm. Show your work.

14. Write the efficiency class of your algorithm in Big-Oh notation.

Solutions

Expert Solution

12) Approach:

Approach is very simple. We will loop for each element in the list. For each element ia n the list, we will check the whole list from that element onwards. If there is any element b such that a*b = 1. If there's any such pair, add it to our result list. So we are greedily checking all pairs of elements in the list and pushing all such pairs where a*b = 1 to our result list.

Pseudocode will look like this:

function ReciprocalSearch(L):
    
    Declare result list, res;
    for i = 1 to n:
        for j = i to n:
            if L[j] * L[i] == 1:
                res.push(pair(L[i], L[j]))


    return res 
end

13)

Step count for algorithm:

The two for loops are nested, so their combined time complexity will be

Then total cost of algorithm is:

Substitute, C1 = c1 + c2 + c5 + c6 and    C2 = c2*c4

14) Our input length is n as we are having n floating point numbers. There are two nested loops. One is running from 1 to n and other is running from i to n. Let's break it further.

For each element, the algorithm loops for elements ahead it. So, for first element the inner loop will run n - 1 times, for second element the inner loop will run n - 2 times, for third element it will run n - 3 times. This will go on and for the last element inner loop will not run at all.

So, time complexity, T(n) = (n - 1) + (n - 1) + (n - 2) + (n - 3)+ ......... + 1

It is just the sum of n - 1 natural numbers, that is (n*(n - 1)) / 2.

T(n) = (n^2 - n) / 2

T(n) = O(n^2).

if it helps you, do upvote as it motivates us a lot!

If you have any doubt, do share in comments section.


Related Solutions

this is a python code: QUESTION 1: Input floating point numbers for a, b, c, d...
this is a python code: QUESTION 1: Input floating point numbers for a, b, c, d and e. calculate the following and display the result.  Mathematical expression ab means a * b in programming context. QUESTION 2: You are sleep expert for a baby that is having struggle sleeping every night. You are to ask the mother of the child "How many oz of milk the child drank ?" Based on the amount of milk, you will have to determine...
Practice basic output formatting by reproducing the output below. All floating-point numbers should have 3 decimal...
Practice basic output formatting by reproducing the output below. All floating-point numbers should have 3 decimal places. Use these constants and values: NUM1= 10, NUM2= 1.49, and NUM3= 12.538767 Output: NUM1 NUM2 NUM3 10 1.490 12.539 ------------------------ 123456789012345678901234 <-- DO NOT output this area. It is here to help you align things. ------------------------ Solve in C++.
[PYTHON] How do you write a program that first gets a list of floating point numbers...
[PYTHON] How do you write a program that first gets a list of floating point numbers from input. The input begins with an integer indicating the number of numbers that follow. Then input all data values and store them in a list [PYTHON]
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits...
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits in length. The exponent field is 5 bits. The significand field is 8 bits. The bias is 15 Represent -32.5010 in the simple model.
Assume that you have a 12-bit floating point number system, similar to the IEEE floating point...
Assume that you have a 12-bit floating point number system, similar to the IEEE floating point standard, with the format shown below and a bias of 7. The value of a floating point number in this system is represented as    FP = (-1)^S X 1.F X 2^(E-bias) for the floating point numbers A = 8.75 and B = -5.375. The binary representation of A is given as A = 0101 0000 1100 Show the hexidecimal representation of B.
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with...
Q1: In the addition of floating-point numbers, how do we adjust the representation of numbers with different exponents? Q2: Answer the following questions: What binary operation can be used to set bits? What bit pattern should the mask have? What binary operation can be used to unset bits? What bit pattern should the mask have? What binary operation can be used to flip bits? What bit pattern should the mask have?
x[n] is the input of a system and y[n] is the output of the system. The...
x[n] is the input of a system and y[n] is the output of the system. The relationship between the input and output is the following: y[n] = x[n]u[n+1] a) Is the system memoryless? Just yes or no is sufficient. b) Is this system causal? Just yes or no is sufficient. c) Is the system linear? Just yes or no is sufficient. d) Is the system time invariant? Justify. e) Is the system BIBO stable? Justify. f) Is the system invertible?...
Convert the following numbers to floating point and show work. -0.063 65.3
Convert the following numbers to floating point and show work. -0.063 65.3
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
Input 100 numbers and find and output how many numbers are positive and negative, find and...
Input 100 numbers and find and output how many numbers are positive and negative, find and output average of these numbers Write using vb.net
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT