1

Okay I really suck at titles! If someone has a better one, please feel free to edit!

Anyway, what I'd like to do is find an equation that maps all pairs $(x,y)$ of integers $0$ to $27$ (inclusive) to an integer $z$ in the same range.

Additional constraints:

  • If $x = y$, then $z = x = y$
  • Otherwise, avoid $z = x$ and $z = y$ if possible
  • Results of $z$ should be reasonably evenly distributed, but some variation is okay
  • For every value of $z$ there must exist at least one pair $(x,y)$ where $x \ne y$

So my initial attempt at solving this was simple enough:

$$z = \sqrt{(x+1) \times (y+1)}-1 \tag{rounded to nearest integer}$$

It's fairly simple to see that the first constraint is met. It doesn't meet the second one but that's not too big of an issue. Results for $z$ were biased towards the middle of the range, but again that's okay. The major flaw is that the only possible solution for $z = 27$ is $(x,y) = (27,27)$, which is not acceptable.

Any help solving this would be greatly appreciated!

1 Answers1

0

I think $z=(2x-y\mod 27)$ will do. (it had to be $\mod 28$ everywhere instead)

  • When $x=y$, you get $z=2x-x=x=y$.
  • If $x=z$, then $x\equiv z\equiv 2x-y\mod 27$. This implies $x\equiv y\mod 27$. If $y=z$, we get $y\equiv z\equiv 2x-y\mod 27$. Now it follows that $2x\equiv 2y\mod 27$, and because $27$ is odd, we get $x=y$. (Since $28$ is not odd, we get $x=y$ or $x=y\pm 14$.)
  • Results will be evenly distributed, because when $f(x,y)=z$, we have $f(x+1,y+1)=z+1$. (For every pair $(x,y)$, we can find a pair with the same difference for any other $z$.)
  • For every $z$ there is a pair $(x,y)$ with $x\neq y$, for example $(z+1,z+2)$ (with both elements modulo $27$).
Ragnar
  • 6,233
  • Since 27 is to be included, I needed to use mod 28 instead, and I also realised that I forgot I also needed $f(x,y) = f(y,x)$, but I fixed that by just switching $x$ and $y$ to be the same way around. Thank you very much for your help! – Niet the Dark Absol Jan 02 '14 at 16:08
  • @NiettheDarkAbsol What do you mean with 'switching $x$ and $y$ the same way around'? What is the formula you get? – Ragnar Jan 02 '14 at 16:11
  • In my code, I do if(x>y) list(x,y) = [y,x];, followed by the formula you gave. – Niet the Dark Absol Jan 02 '14 at 16:16
  • That's a fine solution, although it may disturb the evenly distribution a bit, but I don't think thats a problem. – Ragnar Jan 02 '14 at 16:19
  • I ran it for all values, and the distribution came out the same as it did without flipping: 27 or 29 alternately. Which is fine. Again, thanks for the help! Wonderful solution. Also quite pretty to look at... – Niet the Dark Absol Jan 02 '14 at 16:23
  • With $\bmod 28$ instead of $\bmod 27$ we no longer can use "because $27$ is odd". – Hagen von Eitzen Jan 02 '14 at 16:23
  • @HagenvonEitzen I know, but the OP only asked for almost evenly distributed. – Ragnar Jan 02 '14 at 16:25