AlbaLed Posted October 23, 2003 Share Posted October 23, 2003 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 Quote Link to comment Share on other sites More sharing options...
jaideep Posted October 23, 2003 Share Posted October 23, 2003 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 Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted October 24, 2003 Author Share Posted October 24, 2003 Anyone else want to give 'em a try before I post the answers, Jaideep got some right, not necessarily all, I will not point which though. I'll post the answers in about 2-3 hours AlbaLed Quote Link to comment Share on other sites More sharing options...
crackit Posted October 24, 2003 Share Posted October 24, 2003 answer to 4 is 2*(number of 1 in Z) Quote Link to comment Share on other sites More sharing options...
crackit Posted October 24, 2003 Share Posted October 24, 2003 Originally posted by crackit answer to 4 y*z Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted October 24, 2003 Author Share Posted October 24, 2003 Answers are 1. swaping 2. x power y typo on 3, sorry f(y,z) { x:=1 while z>0 do if [color=Red]z[/color] is odd then x:=x*y z:= floor(z/2) y:=y^2 return (x) } 3. y power z 4. y*z Good job jaideep and crackit AlbaLed Quote Link to comment Share on other sites More sharing options...
bb Posted October 25, 2003 Share Posted October 25, 2003 For swapping 1. has to have x:= x + y y:= x - y x:= x - x Quote Link to comment Share on other sites More sharing options...
bb Posted October 25, 2003 Share Posted October 25, 2003 #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) Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted October 25, 2003 Author Share Posted October 25, 2003 wouldn't x=x-x be 0 ??? it is not x-x because now the previous value of x is in y. AlbaLed Quote Link to comment Share on other sites More sharing options...
bb Posted October 25, 2003 Share Posted October 25, 2003 Sorry for #1. :o) Quote Link to comment Share on other sites More sharing options...
jaideep Posted October 25, 2003 Share Posted October 25, 2003 Alba, Nice on. But I still fell that you answer to second is wrong. Can you recheck it again. I feel answer should be y pow z. Jaideep Quote Link to comment Share on other sites More sharing options...
crackit Posted October 25, 2003 Share Posted October 25, 2003 I agree with jaideep answer to 2 should be y^z. Quote Link to comment Share on other sites More sharing options...
wood Posted October 25, 2003 Share Posted October 25, 2003 True. Answer is definitely y^z. Wood Quote Link to comment Share on other sites More sharing options...
AlbaLed Posted October 25, 2003 Author Share Posted October 25, 2003 True, sorry for the typo AlbaLed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.