Yesterday in order to solve a coding question I had to swap 4 digits in place! Now I knew the formula to swap 2 digits in place, so I thought, the math cannot be that hard for n number of digits can it?
Turns out I was right! I tried creating some formulas and then, ahha! I found it! Really simple one! Here it is:
Say you have variables a,b,c,d and you want to assign values in this fashion:
a->b b->c c->d d->a. This is how you'll do it:
a = a+b+c+d
b = a-b-c-d
c = a-b-c-d
d = a-b-c-d
a = a-b-c-d
This can simply be extended for n number of digits. Easy naa!
Tweet
Turns out I was right! I tried creating some formulas and then, ahha! I found it! Really simple one! Here it is:
Say you have variables a,b,c,d and you want to assign values in this fashion:
a->b b->c c->d d->a. This is how you'll do it:
a = a+b+c+d
b = a-b-c-d
c = a-b-c-d
d = a-b-c-d
a = a-b-c-d
This can simply be extended for n number of digits. Easy naa!
Tweet