Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2008-02-26 05:35:45

mikau
Member
Registered: 2005-08-22
Posts: 1,504

variable swapping puzzle

here's something i thought of today at school

suppose you have two integer values a and b stored in two int variables x and y

int x = a;
int y = b;

normally, if we want to swap the values in these two variables, we need to instantiate a temporary variable to save one of the values during the swap

int temp = x;
x = y;
y = temp;

BUT can YOU think of a way, in Java or C++, to swap the two int values without creating an additional variable?

You may use as many lines of code as you wish but you may not use loops or if statements or the like. You may not call any predefined functions of course. You may use boolean, arrithmetic or logical operators if you wish.

I have tested my solution in both C++ and java to make sure they worked.

Last edited by mikau (2008-02-26 05:42:19)


A logarithm is just a misspelled algorithm.

Offline

#2 2008-02-26 05:42:06

TheDude
Member
Registered: 2007-10-23
Posts: 361

Re: variable swapping puzzle

int x = a;
int y = b;

x = x + y;
y = x - y;
x = x - y;


Wrap it in bacon

Offline

#3 2008-02-26 05:43:55

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: variable swapping puzzle

that was quick!  good job, thedude!

Now, can you do it in one line of code?


A logarithm is just a misspelled algorithm.

Offline

#4 2008-02-26 07:25:14

Ricky
Moderator
Registered: 2005-12-04
Posts: 3,791

Re: variable swapping puzzle

I was going to do inline assembly, bu then I remembered how much work it would be to look up register names and the like.


"In the real world, this would be a problem.  But in mathematics, we can just define a place where this problem doesn't exist.  So we'll go ahead and do that now..."

Offline

#5 2008-02-26 10:06:52

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: variable swapping puzzle

hehehe!

Well here's

for how to do it in one line.


A logarithm is just a misspelled algorithm.

Offline

#6 2008-02-26 10:42:10

luca-deltodesco
Member
Registered: 2006-05-05
Posts: 1,470

Re: variable swapping puzzle

does this actually use less memory though?

if you use the normal method of copying it into a temporary variable, you use 3 int size's memory.

if you use your solution you still have to use at minimum, 3 int size's memory tongue

its still interesting to see things like this though smile is it possible to not use any extra memory? perhaps with a restriction on the values of the integers.

Last edited by luca-deltodesco (2008-02-26 10:44:04)


The Beginning Of All Things To End.
The End Of All Things To Come.

Offline

#7 2008-02-26 11:15:08

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: variable swapping puzzle

the purpose of this puzzle is by no means to save memory or time, its just a puzzle for fun. smile

However, what I like about TheDude's method is you could use it in assembly to swap the values in two registers without having to use an extra one. Pretty cool!

Does anyone else have any fun code wielding challenges? post them here!

Last edited by mikau (2008-02-26 11:15:55)


A logarithm is just a misspelled algorithm.

Offline

#8 2008-02-27 01:09:07

TheDude
Member
Registered: 2007-10-23
Posts: 361

Re: variable swapping puzzle

I'd be wary of using that one line solution, since it can be interpretted two different ways by the compiler.  If it evaluates right to left then it will set x = y first, then subtract that from x + y, which is now y + y, and you'll end up with y = y and x = y.  Of course, I realize that you aren't suggesting actually using such code, but I thought that should be pointed out.


Wrap it in bacon

Offline

#9 2008-02-27 09:19:26

mikau
Member
Registered: 2005-08-22
Posts: 1,504

Re: variable swapping puzzle

TheDude wrote:

I'd be wary of using that one line solution, since it can be interpretted two different ways by the compiler.  If it evaluates right to left then it will set x = y first, then subtract that from x + y, which is now y + y, and you'll end up with y = y and x = y.  Of course, I realize that you aren't suggesting actually using such code, but I thought that should be pointed out.

I suppose it might be interpreted different ways by different compilers, but my understanding of parenthesis is that inner most parens are always evaluated first, thats why i have doubled parens around x + y. So I'm PRETTY sure this would work on any correct compiler.


A logarithm is just a misspelled algorithm.

Offline

#10 2008-03-27 18:03:36

Kargoneth
Member
Registered: 2007-08-11
Posts: 33

Re: variable swapping puzzle

y = x XOR y
x = x XOR y
y = x XOR y

Offline

Board footer

Powered by FluxBB