Find a 4-digit number which, divided by a 3-digit number (all unique digits) equals 9

2.6k Views Asked by At

This question is related to this Stack Overlow post. I tried following R code to find a 4 digit number divided by a 3 digit number (all unique digits) so that result equals 9:

ss = sample(0:9, 7)
while(TRUE){
    print(ss)
    if(ss[4]+10*ss[3]+100*ss[2]+1000*ss[1]/(ss[7]+10*ss[6]+100*ss[5]) == 9){ break }
    ss = sample(0:9, 7)
    }
print("found:")
ss

Program runs for a long time but no such number is found. I will appreciate any information on this. My apologies if this is a trivial issue.

EDIT: This is a programming error with '()'. On correcting it many such numbers are found:

Found:  3708  and  412
Found:  1863  and  207
Found:  6831  and  759
Found:  4689  and  521
Found:  7461  and  829
Found:  8523  and  947
Found:  7569  and  841
Found:  4761  and  529
Found:  5472  and  608
Found:  7506  and  834
Found:  8127  and  903
Found:  4761  and  529
Found:  8253  and  917
Found:  8163  and  907
Found:  3681  and  409
Found:  6381  and  709
Found:  6318  and  702
Found:  6408  and  712
Found:  5427  and  603
Found:  8127  and  903
Found:  3681  and  409
Found:  3672  and  408
Found:  8613  and  957
Found:  3762  and  418
...

Thanks for your help.

4

There are 4 best solutions below

2
On

Instead of looking at $10^7$ possible pairs of two numbers, you can loop over $10^3$ (actually, $900$) three-digit numbers. Multiply each by $9$, and check whether the digits of the product are distinct. To extract the digits, you can divide the product by $10$, take the remainder, and repeat this four times.

0
On

Suppose $9\times abc=defg$. Then we can write $defg+abc=abc0$. Hence $g+c=10$, and other relations must hold as well. For example, since $d\neq a$, we must have $d=a-1$. Using similar logic, I found the following answer by trial and error. There may be other solutions too.

$$2754/306=9$$

2
On

Of course, it can be done in SQL as well (Oracle flavoured).

  WITH
     lvls AS (
        SELECT LEVEL AS lvl
        FROM dual
        CONNECT BY LEVEL < 10000
     )
  SELECT
     l1.lvl AS dg_3,
     l2.lvl AS dg_4
  FROM lvls l1
  JOIN lvls l2 
     ON (   9 * l1.lvl = l2.lvl
        AND NOT REGEXP_LIKE(l1.lvl || l2.lvl, '(\d).*\1'))
  WHERE l1.lvl BETWEEN  100 AND  999
    AND l2.lvl BETWEEN 1000 AND 9999
  ORDER BY l1.lvl

Which finds the same 41 pairs:

$(204,1836),(206,1854),(207,1863),(306,2754),(309,2781),(402,3618),(408,3672),(409,3681),(412,3708),(418,3762),(421,3789),(429,3861),(512,4608),(521,4689),(529,4761),(602,5418),(603,5427),(608,5472),(609,5481),(638,5742),(647,5823),(702,6318),(709,6381),(712,6408),(721,6489),(759,6831),(804,7236),(806,7254),(814,7326),(829,7461),(834,7506),(836,7524),(841,7569),(903,8127),(904,8136),(906,8154),(907,8163),(917,8253),(924,8316),(947,8523),(957,8613)$

2
On

Why dont you just nest two foreach loops where the outer is from [1000:9999] and the inner [100:999] and then ask if the result of the division is 9 for each calc?

Or much faster: Multiply 9 and [100:999] checking if the result is a number between [1000:9999]?