1.Write an algorithm, in English, to solve the following problem. Be sure to include pre- and post-conditions!
2.Problem: Solve X mod Y for all positive integers. Do so without using multiplication or division.
3.What is (7 / 3) + (4 % 5)?
1. Algorithm to solve X mod Y without using multiplication or division:
1. Check if X is less than or equal to Y. If it is, store the value of X as the result and stop.
2. If X is greater than Y, subtract Y from X and update the value of X with the result of the subtraction.
3. Repeat step 2 until X is less than or equal to Y.
4. The final value of X is the result of X mod Y.
Preconditions: X and Y are positive integers, and Y is not equal to zero.
Postconditions: The algorithm should produce an output that represents X mod Y.
2. Algorithm to solve (7/3) + (4%5):
1. Calculate the result of 7 divided by 3.
2. Calculate the remainder of 4 divided by 5.
3. Add the result from step 1 to the result from step 2.
4. The final result is the sum obtained from step 3.
Preconditions: None.
Postconditions: The algorithm should produce the value of (7/3) + (4%5).
1. Algorithm to solve "X mod Y without using multiplication or division":
Pre-conditions:
- X and Y are positive integers
- X > Y
Post-conditions:
- Result is the remainder of dividing X by Y
Steps:
1. Initialize a variable "remainder" to 0
2. Repeat the following until X becomes less than Y:
a. Subtract Y from X
b. If X is still greater than or equal to 0, increment "remainder" by 1
3. Return the value of "remainder" as the result
Example run:
Let's say X = 13 and Y = 5
Step 1: Initialize "remainder" to 0
Step 2: Repeat until X < Y
- Subtract 5 from 13: X = 8, remainder = 1
- Subtract 5 from 8: X = 3, remainder = 2
Step 3: X(3) < Y(5), so stop and return remainder(2) as the result
2. To solve the expression (7 / 3) + (4 % 5) without using division or multiplication, use the following steps:
Step 1: Evaluate the expression within parentheses.
a. (7 / 3) = 2 (floor division)
b. (4 % 5) = 4 (remainder of 4 when divided by 5)
Step 2: Add the results from step 1.
a. 2 + 4 = 6
Therefore, the result of (7 / 3) + (4 % 5) is 6.