-9

How to produce a unique number from combination of 2 numbers in PHP ? For example, combination of these two numbers; 68 and 73, produce X ( X is a unique number). It does not matter how the order of the combination numbers, it should still produce same unique number. E.g: (68,73)=X or (73,68)=X or combination of (1,2)=Y or (2,1)=Y , Y is a different number (because it's unique) from X.

Let me know if you need more information.

I think this problem is similar to my problem but it is written in C#.

I converted this code to PHP :

function getUnique($x, $y){
  if($x>$y){
    return $y | $x << 32;
  }  else{
    return $x | $y << 32;
  }
}

$list = array();
for($i=0 ; $i<100 ;$i++){
  $x = rand(1,10); // I just put any random numbers from 1 to 10 so that I can read it easily.
  $y = rand(1,10);
  $unique = getUnique($x, $y);
  $list[$i] = '('.$x.','.$y.')='.$unique;
 }

//Find out the results
foreach($list as $ans){
   echo $ans.'<br>';
}

But, it does not produce unique numbers. Example from this results:

(2,9)=11

(10,1)=11

(2,9) should produce different number from (10,1).

Community
  • 1
  • 1
  • Is there a maximum for the input numbers (for example 100)? Such that you would never get (37,205). If not, no. If yes, then absolutely. – jedwards May 20 '12 at 02:57
  • 2
    What do you mean by a unique number? – None May 20 '12 at 03:02
  • Are the numbers always integers, as in your examples? – Junuxx May 20 '12 at 03:09
  • are you looking for the value of X and Y. like 12+24= **36** and any combination of numbers that sum **36** your question is confusing. – Lawrence Cherone May 20 '12 at 03:11
  • @jedwards :There is no maximum input number. – user1404631 May 20 '12 at 03:44
  • Then no, it is not possible. It would be possible if you specified a maximum input, otherwise unfortunately no. – jedwards May 20 '12 at 03:45
  • This question seems legit. What's the reason to close? Anyway I have added a new answer in the same SO thread you posted which addresses your concern. Or see this link http://stackoverflow.com/a/13871379/661933 – nawfal Dec 14 '12 at 14:05

1 Answers1

2

No, at least not in general (though there may be if you specify some restrictions).

Assuming n-bit types, there are 2n possible values. But there are 22n possible pairs of values. Obviously there's not a unique mapping from a pair to a value.

Of course, if you're prepared to have an output type that's wider than your input type, then this is trivial.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650