A coworker at lunch at work told us that his 2nd grade son had been given a problem something like this:

_ _ _ - _ _ _ = _ _ _

Fill in the blanks to create a… valid? true? equation, using only the numbers 1-9 exactly once each. We talked about this for a while, and none of us could come to a good answer over lunch. I know that I was always being given problems like this as a kid and never knew how to solve them. Long story short, when I got back to my desk, I wrote a little C# program to solve it. Here’s the program, along with its output.

The interesting thing to me is that on the left side, at least, the numbers tend to bunch together. Like, there’s a lot of solutions that include 459, 468, and 495, but none for any other numbers less than 459. This suggests to me that there’s some kind of pattern or algorithm for figuring out valid answers. I might have to come back to this.

public static bool NextPermutation(int[] array)
{
    // NextPermutation code from https://www.nayuki.io/res/next-lexicographical-permutation-algorithm/nextperm.cs
    // Find non-increasing suffix
    int i = array.Length - 1;
    while (i > 0 && array[i - 1] >= array[i])
        i--;
    if (i <= 0)
        return false;

    // Find successor to pivot
    int j = array.Length - 1;
    while (array[j] <= array[i - 1])
        j--;
    int temp = array[i - 1];
    array[i - 1] = array[j];
    array[j] = temp;

    // Reverse suffix
    j = array.Length - 1;
    while (i < j)
    {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        i++;
        j--;
    }
    return true;
} 

static int Numberize(IEnumerable<int> num)
{
    //C# doesn't have an easy way to slice arrays, but this is basically the same thing.

    int toReturn = 0;
    int multiplier = 100;

    foreach (int number in num)
    {
        toReturn += number * multiplier;
        multiplier = multiplier / 10;
        if (multiplier < 1)
            break;
    }

    return toReturn;
}

//We have to reverse the array because "987654321" is the last permutation.
//You can start with that if you want- it was easier to make a copy when I was prototyping.
int[] numbers = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
reversedNumbers = numbers.Reverse().ToArray();
 do
 {
     int first = Numberize(reversedNumbers);
     int second = Numberize(reversedNumbers.Skip(3));
     int third = Numberize(reversedNumbers.Skip(6));
     if ((first - second) == third)
         Console.WriteLine(string.Format("{0} - {1} = {2}", first, second, third));
 } while (NextPermutation(reversedNumbers));


/*
Output:
459 - 173 = 286
459 - 176 = 283
459 - 183 = 276
459 - 186 = 273
459 - 273 = 186
459 - 276 = 183
459 - 283 = 176
459 - 286 = 173
468 - 173 = 295
468 - 175 = 293
468 - 193 = 275
468 - 195 = 273
468 - 273 = 195
468 - 275 = 193
468 - 293 = 175
468 - 295 = 173
486 - 127 = 359
486 - 129 = 357
486 - 157 = 329
486 - 159 = 327
486 - 327 = 159
486 - 329 = 157
486 - 357 = 129
486 - 359 = 127
495 - 127 = 368
495 - 128 = 367
495 - 167 = 328
495 - 168 = 327
495 - 327 = 168
495 - 328 = 167
495 - 367 = 128
495 - 368 = 127
549 - 162 = 387
549 - 167 = 382
549 - 182 = 367
549 - 187 = 362
549 - 362 = 187
549 - 367 = 182
549 - 382 = 167
549 - 387 = 162
567 - 128 = 439
567 - 129 = 438
567 - 138 = 429
567 - 139 = 428
567 - 218 = 349
567 - 219 = 348
567 - 248 = 319
567 - 249 = 318
567 - 318 = 249
567 - 319 = 248
567 - 348 = 219
567 - 349 = 218
567 - 428 = 139
567 - 429 = 138
567 - 438 = 129
567 - 439 = 128
576 - 182 = 394
576 - 184 = 392
576 - 192 = 384
576 - 194 = 382
576 - 382 = 194
576 - 384 = 192
576 - 392 = 184
576 - 394 = 182
594 - 216 = 378
594 - 218 = 376
594 - 276 = 318
594 - 278 = 316
594 - 316 = 278
594 - 318 = 276
594 - 376 = 218
594 - 378 = 216
639 - 152 = 487
639 - 157 = 482
639 - 182 = 457
639 - 187 = 452
639 - 452 = 187
639 - 457 = 182
639 - 482 = 157
639 - 487 = 152
648 - 251 = 397
648 - 257 = 391
648 - 291 = 357
648 - 297 = 351
648 - 351 = 297
648 - 357 = 291
648 - 391 = 257
648 - 397 = 251
657 - 218 = 439
657 - 219 = 438
657 - 238 = 419
657 - 239 = 418
657 - 418 = 239
657 - 419 = 238
657 - 438 = 219
657 - 439 = 218
675 - 182 = 493
675 - 183 = 492
675 - 192 = 483
675 - 193 = 482
675 - 281 = 394
675 - 284 = 391
675 - 291 = 384
675 - 294 = 381
675 - 381 = 294
675 - 384 = 291
675 - 391 = 284
675 - 394 = 281
675 - 482 = 193
675 - 483 = 192
675 - 492 = 183
675 - 493 = 182
693 - 215 = 478
693 - 218 = 475
693 - 275 = 418
693 - 278 = 415
693 - 415 = 278
693 - 418 = 275
693 - 475 = 218
693 - 478 = 215
729 - 143 = 586
729 - 146 = 583
729 - 183 = 546
729 - 186 = 543
729 - 543 = 186
729 - 546 = 183
729 - 583 = 146
729 - 586 = 143
738 - 142 = 596
738 - 146 = 592
738 - 192 = 546
738 - 196 = 542
738 - 542 = 196
738 - 546 = 192
738 - 592 = 146
738 - 596 = 142
783 - 124 = 659
783 - 129 = 654
783 - 154 = 629
783 - 159 = 624
783 - 214 = 569
783 - 219 = 564
783 - 264 = 519
783 - 269 = 514
783 - 514 = 269
783 - 519 = 264
783 - 564 = 219
783 - 569 = 214
783 - 624 = 159
783 - 629 = 154
783 - 654 = 129
783 - 659 = 124
792 - 134 = 658
792 - 138 = 654
792 - 154 = 638
792 - 158 = 634
792 - 634 = 158
792 - 638 = 154
792 - 654 = 138
792 - 658 = 134
819 - 243 = 576
819 - 246 = 573
819 - 273 = 546
819 - 276 = 543
819 - 352 = 467
819 - 357 = 462
819 - 362 = 457
819 - 367 = 452
819 - 452 = 367
819 - 457 = 362
819 - 462 = 357
819 - 467 = 352
819 - 543 = 276
819 - 546 = 273
819 - 573 = 246
819 - 576 = 243
837 - 142 = 695
837 - 145 = 692
837 - 192 = 645
837 - 195 = 642
837 - 241 = 596
837 - 246 = 591
837 - 291 = 546
837 - 296 = 541
837 - 541 = 296
837 - 546 = 291
837 - 591 = 246
837 - 596 = 241
837 - 642 = 195
837 - 645 = 192
837 - 692 = 145
837 - 695 = 142
846 - 317 = 529
846 - 319 = 527
846 - 327 = 519
846 - 329 = 517
846 - 517 = 329
846 - 519 = 327
846 - 527 = 319
846 - 529 = 317
864 - 125 = 739
864 - 129 = 735
864 - 135 = 729
864 - 139 = 725
864 - 271 = 593
864 - 273 = 591
864 - 291 = 573
864 - 293 = 571
864 - 571 = 293
864 - 573 = 291
864 - 591 = 273
864 - 593 = 271
864 - 725 = 139
864 - 729 = 135
864 - 735 = 129
864 - 739 = 125
873 - 214 = 659
873 - 219 = 654
873 - 254 = 619
873 - 259 = 614
873 - 614 = 259
873 - 619 = 254
873 - 654 = 219
873 - 659 = 214
891 - 234 = 657
891 - 237 = 654
891 - 254 = 637
891 - 257 = 634
891 - 324 = 567
891 - 327 = 564
891 - 364 = 527
891 - 367 = 524
891 - 524 = 367
891 - 527 = 364
891 - 564 = 327
891 - 567 = 324
891 - 634 = 257
891 - 637 = 254
891 - 654 = 237
891 - 657 = 234
918 - 243 = 675
918 - 245 = 673
918 - 273 = 645
918 - 275 = 643
918 - 342 = 576
918 - 346 = 572
918 - 372 = 546
918 - 376 = 542
918 - 542 = 376
918 - 546 = 372
918 - 572 = 346
918 - 576 = 342
918 - 643 = 275
918 - 645 = 273
918 - 673 = 245
918 - 675 = 243
927 - 341 = 586
927 - 346 = 581
927 - 381 = 546
927 - 386 = 541
927 - 541 = 386
927 - 546 = 381
927 - 581 = 346
927 - 586 = 341
936 - 152 = 784
936 - 154 = 782
936 - 182 = 754
936 - 184 = 752
936 - 752 = 184
936 - 754 = 182
936 - 782 = 154
936 - 784 = 152
945 - 162 = 783
945 - 163 = 782
945 - 182 = 763
945 - 183 = 762
945 - 317 = 628
945 - 318 = 627
945 - 327 = 618
945 - 328 = 617
945 - 617 = 328
945 - 618 = 327
945 - 627 = 318
945 - 628 = 317
945 - 762 = 183
945 - 763 = 182
945 - 782 = 163
945 - 783 = 162
954 - 216 = 738
954 - 218 = 736
954 - 236 = 718
954 - 238 = 716
954 - 271 = 683
954 - 273 = 681
954 - 281 = 673
954 - 283 = 671
954 - 671 = 283
954 - 673 = 281
954 - 681 = 273
954 - 683 = 271
954 - 716 = 238
954 - 718 = 236
954 - 736 = 218
954 - 738 = 216
963 - 215 = 748
963 - 218 = 745
963 - 245 = 718
963 - 248 = 715
963 - 715 = 248
963 - 718 = 245
963 - 745 = 218
963 - 748 = 215
972 - 314 = 658
972 - 318 = 654
972 - 354 = 618
972 - 358 = 614
972 - 614 = 358
972 - 618 = 354
972 - 654 = 318
972 - 658 = 314
981 - 235 = 746
981 - 236 = 745
981 - 245 = 736
981 - 246 = 735
981 - 324 = 657
981 - 327 = 654
981 - 354 = 627
981 - 357 = 624
981 - 624 = 357
981 - 627 = 354
981 - 654 = 327
981 - 657 = 324
981 - 735 = 246
981 - 736 = 245
981 - 745 = 236
981 - 746 = 235
*/