zhabibek4u

zhabibek4u

Lv1

Bibek JhaGalgotias University - GU

0 Followers
0 Following
0 Helped

ANSWERS

Published7

Subjects

Science3Computer Science4
Answer: The code var orOperation = x || y; is not considered dead code. Dead c...

The Problem: Implement the recursive multiplication algorithm in Lecture 8 (slide 14 ) and Section 5.5 (p. 233 ) of our textbook. Note: test your code carefully - subtle considerations need to be taken with stopping conditions, which the textbook does not address.

The Recursive Multiplication:
Algorithm:
1: function Recursive-Multiply (x,y)
2: x=2^(⌊n/2⌋)x_h + x_ℓ
3: y=2^(⌊n/2⌋)y_h + y_ℓ
4: c_x = x_h + x_ℓ
5: c_y = y_h + y_ℓ
6: z_h = Recursive-Multiply (x_h, y_h)
7: z_c = Recursive-Multiply (c_x, c_y)
8: z_ℓ = Recursive-Multiply (x_ℓ, y_ℓ)
9: return 2^(2⌊n/2⌋)z_h + 2(⌊n/2⌋)(z_c − z_h − z_ℓ) + z_ℓ
- Note that we can perform multiplication by 2^x via left shift
- We can also obtain x_h, x_ℓ, y_h, and y_ℓ with AND and MOD
- These are O(n), as is the addition we will need to use (i.e. we have a constant number of O(n) operations, and thus have O(n) runtime overall at the level of each function call)
- Since we split the input in half and now have only three recursive calls, our recurrence becomes T(n)=3T(n/2)+O(n)
- And this has O(n^(log_2(3))) ≈ O(n^1.59), which strictly improves upon the 'grade-school' multiplication

Important: Do not use multi-threading, or any other form of parallelism, for the individual recursive function calls. The grading program expects your output in a format produced by a recursive but non-parallel algorithm. Use of parallelism in function calls will cause the autograder to deem your solution incorrect.

The Input Format: Your program will be expected to read input from a text file, the name of which should be passed as the first parameter to your program on command line invocation. All 'integers' discussed from here on out can be expected to be UTF-8 string representations of said numbers (i.e. as one would find in a .csv file). The input will come in two lines, each representing a single positive integer.

Your Program's Output: Your program will be expected to output a text file named output.txt, in UTF-8 format as described above. Call the number on the first line of the input file x, and the number on the second line of the input file y. Then each line of the output file will contain the state of one of your function calls just before the non-stopping condition return call. This line will contain the following positive integers, in the order indicated, with each separated by a single comma: n_s, x_h, x_ℓ, y_h, y_ℓ, z_h, z_c, z_l where:
- n_s is the number of bits at which you split the inputs
- x_h and y_h are the high bits of x and y respectively
- x_ℓ and y_ℓ are the low bits of x and y respectively
- z_h is the result returned by your recursive multiplication call on x_h ⋅ y_h
- z_c is the result returned by your recursive multiplication call on (x_h + x_ℓ ) ⋅ (y_h + y_ℓ)
- z_l is the result returned by your recursive multiplication call on x_ℓ ⋅ y_ℓ
- That is, we can write:
- x = 2^(n_s) x_h + x_ℓ
- y=2^(n_s) y_h + y_ℓ
- x ⋅ y = z = 2^(2n_s) z_h + 2^(n_s)(z_c − z_h − z_ℓ) + z_ℓ

Some points:
- The input and output format is in (UTF-8\ASCII string) base-10.
- The following examples assume you treat the numbers as binary once they are stored as unsigned integers
- When you split the input, the difference between between the number of bits in the high half and low half should be one if n is odd, and zero if n is even
- The following examples choose the value of n_s based on the minimum of x and y
- The following examples assume that n_s ≤1 is the stopping condition if for odd n, the lower half has more bits
- The following examples assume that n_s = 0 is the stopping condition if for odd n, the higher half has more bits
- The recursions were called in this order: (a) First center, z_c, (b) Second high, z_h, (c) Third low, z_ℓ

Examples:
Here is what the output looks like if for odd n, the lower half has more bits:

Input:
17
11
Output:
2, 1, 1, 1, 1, 1, 4, 1
2, 4, 1, 2, 3, 8, 25, 3

Input:
45
27
Output:
2, 2, 2, 1, 2, 2, 12, 4
3, 5, 5, 3, 3, 15, 60, 15

Input:
192
237
Output:
2, 3, 0, 6, 3, 18, 27, 0
2, 3, 0, 3, 2, 9, 15, 0
14, 12, 0, 14, 13, 168, 324, 0

Write code in Python (python 3.8.10)

Answer: def recursive_multiply(x, y): # Convert x and y to integers x = int(x)...
Answer:The 'OS' computer abbreviation usually means (C) Operating System. Step...
Answer:The '.MOV' extension typically refers to a (B) Animation/movie file. St...
Answer: The correct answer is: A. (Convex) wider field of vision. (Concave) a ...
Answer:Calcium is a chemical element with the symbol Ca and atomic number 20. ...
Answer: Na2CO3Step-by-step explanation: 1. The chemical formula of washing sod...

Weekly leaderboard

Start filling in the gaps now
Log in