Question

In: Computer Science

Mystery(y, z: positive integer) 1 x=0 2 while z > 0 3       if z mod 2...

Mystery(y, z: positive integer)

1 x=0

2 while z > 0

3       if z mod 2 ==1 then

4                x = x + y

5       y = 2y

6       z = floor(z/2)           //floor is the rounding down operation

7 return x

Simulate this algorithm for y=4 and z=7 and answer the following questions:

(3 points) At the end of the first execution of the while loop, x=_____, y=______ and z=_______.

(3 points) At the end of the second execution of the while loop, x=_____, y=______ and z=_______.

(3 points) At the end of the third execution of the while loop, x=_____, y=______ and z=_______.

(2 points) Value returned by the algorithm = __________________

(2 points) What does this algorithm compute?   _________________

Solutions

Expert Solution

Given Algorithm is

1 x=0

2 while z > 0

3       if z mod 2 ==1 then

4                x = x + y

5       y = 2y

6       z = floor(z/2)           //floor is the rounding down operation

7 return x

Input are : z = 7 and y = 4.

First Iteration -

Step 3 is (7 mod 2 == 1) is true as mod operator calculates reminder when 7 is divided by 2.

Step 4 is x = 0 + 4 = 4

Step 5 is y = 2 * 4 = 8

Step 6 is z = floor(7/2) evaluates to 3.

So at the end of first iteration x = 4, y = 8 and z = 3

Second Iteration -

Step 3 is (3 mod 2 == 1) is true as mod operator calculates reminder when 3 is divided by 2.

Step 4 is x = 4 + 8 = 12

Step 5 is y = 2 * 8 = 16

Step 6 is z = floor(3/2) evaluates to 1.

So at the end of Second iteration x = 12, y = 16 and z = 1

Third Iteration -

Step 3 is (1 mod 2 == 1) is true as mod operator calculates reminder when 1 is divided by 2.

Step 4 is x = 12 + 16 = 28

Step 5 is y = 2 * 16 = 32

Step 6 is z = floor(1/2) evaluates to 0.

So at the end of Third iteration x = 28, y = 32 and z = 0

Loop ends.

Finally the algorithm returns x value as 28 which is the multipled value of z(=7) and y (=4).


Related Solutions

Let x, y ∈ Z. Prove that x ≡ y + 1 (mod 2) if and...
Let x, y ∈ Z. Prove that x ≡ y + 1 (mod 2) if and only if x ≡ y + 1 (mod 4) or x ≡ y + 3 (mod 4)
Find the smallest positive integer x that satisfies the system of congruences x ≡ 3 (mod...
Find the smallest positive integer x that satisfies the system of congruences x ≡ 3 (mod 5). x ≡ 5 (mod 7). x ≡ 7 (mod 11)
1A) Use surface integral to evaluate the flux of F(x,y,z) =<x^3,y^3,z^3> across the cylinder x^2+y^2=1, 0<=z<=2...
1A) Use surface integral to evaluate the flux of F(x,y,z) =<x^3,y^3,z^3> across the cylinder x^2+y^2=1, 0<=z<=2 1B) Use the Divergence Theorem to evaluate the flux of F(x,y,z) =<x^3,y^3,z^3> across the cylinder x^2+y^2=1, 0<=z<=2
Verify stokes theorem when S=(x,y,z): 9x^2+y^2=z^2 and 0 ≤z ≤2 and F(x,y,z)=0i+((9x^2)/2)j+((y^(3)*z)/3)k
Verify stokes theorem when S=(x,y,z): 9x^2+y^2=z^2 and 0 ≤z ≤2 and F(x,y,z)=0i+((9x^2)/2)j+((y^(3)*z)/3)k
Verify stokes theorem when S=(x,y,z): 9x^2+y^2=z^2 and 0 ≤z ≤2 and F(x,y,z)=0i+((9x^2)/2)j+((y^(3)*z)/3)k
Verify stokes theorem when S=(x,y,z): 9x^2+y^2=z^2 and 0 ≤z ≤2 and F(x,y,z)=0i+((9x^2)/2)j+((y^(3)*z)/3)k
Find the volume of D= {(x,y,z): x^2+y^2+z^2<_ 4, x _>0, y_>0
Find the volume of D= {(x,y,z): x^2+y^2+z^2<_ 4, x _>0, y_>0
prove that for x is an odd, positive integer, 3x ≡−1 (mod 4). I'm not sure...
prove that for x is an odd, positive integer, 3x ≡−1 (mod 4). I'm not sure how to approach the problem. I thought to assume that x=2a+1 and then show that 3^x +1 is divisible by 4 and thus congruent to 3x=-1(mod4) but I'm stuck.
Given function f(x,y,z)=x^(2)+2*y^(2)+z^(2), subject to two constraints x+y+z=6 and x-2*y+z=0. find the extreme value of f(x,y,z)...
Given function f(x,y,z)=x^(2)+2*y^(2)+z^(2), subject to two constraints x+y+z=6 and x-2*y+z=0. find the extreme value of f(x,y,z) and determine whether it is maximum of minimum.
Velocity field for this system is: V=[X^2-(y*z^1/2/t)]i-[z*y^3+(x^1/3*z^2/t^1/2)j+[-x^1/3*t^2/z*y^1/2]k find the components of acceleration for the system.
Velocity field for this system is: V=[X^2-(y*z^1/2/t)]i-[z*y^3+(x^1/3*z^2/t^1/2)j+[-x^1/3*t^2/z*y^1/2]k find the components of acceleration for the system.
Find all x ∈ Z such that x≡2 mod 221 and x≡5 mod 184.
Find all x ∈ Z such that x≡2 mod 221 and x≡5 mod 184.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT