2
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ I am concerned about the bracket placement on line 4. Does the four digit number not need brackets? ie: it looks as though ss[1]/(3 digit number) is performed and then ss[4]...ss[2] is added to the result. Which is unlikely to ever be 9. $\endgroup$ – Nic Sep 24 '14 at 4:28
  • 2
    $\begingroup$ Even if you loop over $10^7$ possibilities, you should still get an answer in a reasonable amount of time. Adding a ')' after 'ss[1]' might fix your program. $\endgroup$ – JimmyK4542 Sep 24 '14 at 4:35
  • $\begingroup$ I don't see the requirement you posted in my answer in your code.. I think you need an algo rewrite. My post should help. GL if you are still searching! $\endgroup$ – BAR Oct 5 '14 at 2:25
9
$\begingroup$

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$$

$\endgroup$
5
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ Using this method, we find the following solutions: (1836,204) (1854,206) (1863,207) (2754,306) (2781,309) (3618,402) (3672,408) (3681,409) (3708,412) (3762,418) (3789,421) (3861,429) (4608,512) (4689,521) (4761,529) (5418,602) (5427,603) (5472,608) (5481,609) (5742,638) (5823,647) (6318,702) (6381,709) (6408,712) (6489,721) (6831,759) (7236,804) (7254,806) (7326,814) (7461,829) (7506,834) (7524,836) (7569,841) (8127,903) (8136,904) (8154,906) (8163,907) (8253,917) (8316,924) (8523,947) (8613,957) $\endgroup$ – JimmyK4542 Sep 24 '14 at 4:33
  • 1
    $\begingroup$ One command in Mathematica: {#, 9 #} & /@ Select[FromDigits /@ Select[Permutations[Range[10] - 1, {3}], #[[1]] != 0 &], Length[Union[Flatten[IntegerDigits /@ {#, 9 #}]]] == 7 &] $\endgroup$ – heropup Sep 24 '14 at 12:43
3
$\begingroup$

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)$

$\endgroup$
  • $\begingroup$ Much credit to you knowing how do anything like this in sql. $\endgroup$ – BAR Sep 24 '14 at 9:01
  • 2
    $\begingroup$ Not enough jQuery. $\endgroup$ – user147263 Sep 24 '14 at 15:06
1
$\begingroup$

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]?

$\endgroup$
  • 1
    $\begingroup$ One needs to get only those numbers where the digits are not repeated. $\endgroup$ – rnso Sep 24 '14 at 12:41
  • $\begingroup$ Divide each result of the above by increasing powers of 10 (10^[1:3]), then take the modulo 10 of that or simply cast the double to an int to isolate the digit value. Store single digit values in an array and associate with full value in a map. Check subsequent results against the stored array. If you know bit arrays you can use that instead of the single digit array. Hope this helps. $\endgroup$ – BAR Sep 25 '14 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.