Our Community is 940,000 Strong. Join Us.


Math puzzler


2Slow4U_Noob
07-08-2004, 12:00 PM
I was going to post a couple of riddles or puzzlers that I have heard, but I'm sure everyone has heard of them before so heres a math/computer question asked to me at an interview...I wasnt able to solve it right away but I got the job any because my cousin worked there :iceslolan


Puzzler:

If you have two variables X and Y. What is the algorithm/How can you swap their values without using a third variable?

EX:
X=25 Y=7
how can you get
X=7 and Y=25
without using another variable.

boingo82
07-08-2004, 12:14 PM
In the same equation? I don't think you are allowed to change the value of variables in an equation.

Jet-Lee
07-08-2004, 12:19 PM
If X = "25" And _
Y = "7"Then
Set X = "7" And_
Set Y = "25"
End If

hehehe...how's that?

psychobadboy
07-08-2004, 12:25 PM
25 and 7 were example numbers. That wouldn't work for other values of x and y.

I'm working on it...

2Slow4U_Noob
07-08-2004, 12:27 PM
In the same equation? I don't think you are allowed to change the value of variables in an equation.


Yes in the same equation. So if you were allowed to use an extra variable the answer would be something like this.

Swapping X and Y
Z=X
X=Y
Y=Z

using numbers as an ex:
X=25
Y=7
Z=0

1)Z=X so Z=25
2)X=Y so X=7
3)Y=Z so Y=25
and now X and Y have switched values

without Z's help you cant do this because after
X=Y or X=7
you would lose the value of X or 25 to give to Y

Wow this was supposed to be just a lil teazer but i think its just making it clear that I'm a massive dork :(

Raz_Kaz
07-08-2004, 12:28 PM
lol, close one there lee, but no cigar
logarithmes are gay, lol, I just finished school and here he comes with more school work :banghead:

2Slow4U_Noob
07-08-2004, 12:29 PM
If X = "25" And _
Y = "7"Then
Set X = "7" And_
Set Y = "25"
End If

hehehe...how's that?


hahaha cheater :iceslolan I knew someone was goin to do that

psychobadboy
07-08-2004, 12:38 PM
Answer:
y=x-y
x=x-y
y=x+y

2Slow4U_Noob
07-08-2004, 12:45 PM
Answer:
y=x-y
x=x-y
y=x+y


Well done! :bigthumb:

I read ur profile and figured ud get it pretty soon

Interests: Cars and computers
Biography: Male computer engineering college student from Southern Cali

psychobadboy
07-08-2004, 12:51 PM
:thumbsup:

Add your comment to this topic!