# What is my friend's PIN?

Just a fun Puzzle:

My (fictive) friend told me his PIN: "My PIN is the highest 4 digit number that has the highest sum of unique differences."

He explained: "my PIN consists of 4 numbers A B C D.

• Calculate the absolute differences between A and B, A and C, A and D, B and C, B and D, C and D (so all differences)
• Remove duplicates (so each difference is included only once)
• Add the remaining differences together.

My PIN is the 4 digit number where this count is the highest and the highest of those numbers. "

What is my (fictive) friend's PIN?

Example:

The number 1469 has a count of:

1 ) calculate the differences: $|1-4| = 3, |1-6| = 5, |1-9| = 8, |4-6| = 2, |4-9| = 5, |6-9| = 3$

2) remove the duplicates: $3, 5, 8, 2$

3) add them together: $18$

So if there is no 4 digit number with a count higher than 18 and 1469 is the highest number of these numbers then 1469 is my friend's pin. (but as I know both conditions are not met)

What is my friend's PIN?

In your answer please describe how you found his PIN (a clever way, or just brute force?)

• *mutter mutter* personal identification number number *mutter* Mar 20 '15 at 19:50
• @DavidRicherby, I came to this question with the express intention of pointing that out. Good to know I'm not the only pedant out there. :) Mar 20 '15 at 20:24
• @DavidRicherby I was going to edit the question, but decided to leave it be when I saw your comment. Mar 18 '16 at 14:50

9820

Reason:

4 numbers, they should have the biggest possible difference of it's members. So take 2 lowest and 2 highest possible numbers: 0, 1, 8 and 9.
But $|8-9|=1$, so let's take another number so that we can use all 6 differences. Next possibility would then be: 0, 1, 7 and 9(1) or 0, 2, 8 and 9(2)
Calculate the difference(1):
$|0-1| = 1, |0-7| = 7, |0-9| = 9, |1-7| = 6, |1-9| = 8, |7-9| = 2$
Numbers are: 1, 7, 9, 6, 8, 2. All 6 are different, so add them up and we get:
$1+7+9+6+8+2=33$
Calculate the difference(2):
$|0-2| = 2, |0-8| = 8, |0-9| = 9, |2-8| = 6, |2-9| = 7, |8-9| = 1$
Numbers are: 2, 8, 9, 6, 7, 1. We can see that differences are same as in 1, but just in different order. So if we add them up, we will still get $33$.
Now we should see which of those 4 is bigger: 0179 or 0289. To make it bigger we should re-order it in descending order, so we get:
1: 9710
2: 9820
and because $9820>9710$, we can say that his PIN code is indeed $9820$

• It's not immediately obvious that "using all 6 differences" is necessary. Mar 20 '15 at 20:04

(I assume that each of the four numbers $A,B,C,D$ is a digit in the range $0,\ldots,9$.)

Since the six differences do not depend on the ordering of $A,B,C,D$, let us assume without loss of generality that $A\le B\le C\le D$, so that the differences are

(1) $B-A$, $C-B$, $D-C$
(2) $C-A$, $D-B$, $D-A$.

The total contribution of the three values in (1) is at most their sum $(B-A)+(C-B)+(D-C)=D-A\le 9-0=9$. The total contribution of the three values in (2) is at most $9+8+7$ (in case they are all different; in case two are the same, the bound is even smaller). From these two upper bounds we conclude:

The count is at most $9+9+8+7=33$.

In order to reach this count, all inequalities in the above discussion must in fact be equalities. This implies $D-A=9$ and hence $A=0$ and $D=9$. Furthermore, the only ways of reaching differences $7$ and $8$ are

• $A=0$, $B=1$, $C=7$, $D=9$
• $A=0$, $B=2$, $C=8$, $D=9$

Together with these two possibilities, all their permutations are also feasible. Hence the highest possible corresponding number (and the pin number of your fictional friend) is the number 9820.

9820

This answer was generated by the following program:

$bigval = 0;$biggest = array();
for($i=1000;$i<10000;$i++) {$pin = (string)$i;$diffs = array();
$diffs[] = abs($pin - $pin);$diffs[] = abs($pin -$pin);
$diffs[] = abs($pin - $pin);$diffs[] = abs($pin -$pin);
$diffs[] = abs($pin - $pin);$diffs[] = abs($pin -$pin);
if(array_sum(array_unique($diffs)) >$bigval) {
$bigval = array_sum(array_unique($diffs));
$biggest = array($pin);
}
else if(array_sum(array_unique($diffs)) ==$bigval) {
$biggest[] =$pin;
}
}
print_r(\$biggest);


Which gave the following output pins, with all the pins having a difference of 33:

1079, 1097, 1709, 1790, 1907, 1970, 2089, 2098, 2809, 2890, 2908, 2980, 7019, 7091, 7109, 7190, 7901, 7910, 8029, 8092, 8209, 8290, 8902, 8920, 9017, 9028, 9071, 9082, 9107, 9170, 9208, 9280, 9701, 9710, 9802, 9820

Of course the answer provided is the highest of these.

The answer is already found by Tryth and Novarg. So for variety sake adding my approach. 0, 9 and 8 are definite entrant due to range. Then I randomly chose 7 as remaining number and formed square:

   0  7  8  9
9  9  2  1
8  8  1
7  7
0


Giving 27.

   0  7  8  9
9  9  2  1
8  8  1
7  7
0


Next

   0  6  8  9
9  9  3  1
8  8  2
6  6
0


Giving 29 ... better.

Next

   0  5  8  9
9  9  4  1
8  8  3
5  5
0


Giving 30 better

Next

   0  4  8  9
9  9  5  1
8  8 
4  4
0


Next

   0  3  8  9
9  9  6  1
8  8  5
3  3
0


Giving 32 better

   0  2  8  9
9  9  7  1
8  8  6
2  2
0


Giving 33 and including 9 8 7 6 2 1

Going further down is going to repeat digits and getting 9 8 7 6 5 1 is not possible, so this one is the answer with largest number 9820

========================================================

Update (a more mathematical approach):

If in the descending order the digits are A>B>C>D

Then the differences are:
A-B,A-C,A-D;B-C,B-D;C-D
Adding all of them gives us:
Total Sum= 3*A+B-C-3*D
Hence for the greatest sum, A & B should be as large as possible
and C & D should be as small as possible.
Which gives first winner combination 9810 but it fails as 9-8 = 1-0
So next one is 9820 which gives no overlap.