Jump to content
Urch Forums

What do this functions do??


AlbaLed

Recommended Posts

Can you find out what do this functions do? Can you prove it? Don't worry about parameter passing methods, just what happens inside the function

1.
f(x,y)
{
   x:=x+y
   y:=x-y
   x:=x-y
}

2. what's returned?
f(y,z)
{
   x:=1
   while z>0 do
       x:=x*y
       z:=z-1
  return (x)
}

3. what's returned?
f(y,z)
{
   x:=1
   while z>0 do
      if x is odd then x:=x*y
       z:= floor(z/2)
       y:=y^2
  return (x)
}

4. what's returned?
f(y,z)
{
   x:=0
   while z>0 do
       x:=x+y*(z mod 2)
       y:=2y
       z:=floor(z/2)
  return (x)
}

 

 

Enjoyy!!!!!!!!!

 

AlbaLed

Link to comment
Share on other sites

1) Swapping

2) y power z

3) This is my guess y (even) then x = y^(2^(|_ lg n _| + 1) - 1)

y = odd then x = y.

4) Convert the z value to its binary equivalent. Start from right side as y then move left as 2.y further left as y.2^2 ans so on. Each time you encounter a one in the binary multiply by its equivalent y factor and add it to the sum.

 

e.g 1 1 1 1

2.2.2.y 2.2.y 2.y y

 

This gives me another idea to state the same thing.

Write down the expression for converting a binary number represented by z to its equivalent decimal number.

Then instead of multiplying by 2^i at the places multiply it by y.2^i.

 

Nice one.

Jaideep

 

Link to comment
Share on other sites

#2.

 

Let's say z:=5

 

z:= 5 ( >0) x := 1*y z:=5-1 = 4

z:=4 ( >0) x ..> 1*y*y z:=4-1 = 3

z:=3 ( >0) x ..> 1*y*y*y z:=3-1 = 2

z:=2 ( >0) x ..> 1*y*y*y*y z:=2-1 = 1

z:=1 ( >0) x ..> 1*y*y*y*y*y z:=1-1 = 0

z:=0 not >0 OUT

 

x = y^z (not x^y)

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...