Message Boards Message Boards

GROUPS:

Digit puzzle: ratio of 5 and 4 digits gives digits 1-9

Posted 4 days ago
185 Views
|
6 Replies
|
4 Total Likes
|

Hello, I am stuck with one question. I have no idea how to turn this into the code. I will need to create a function and use all digits from 1 to 9 to form a five-digit (on top) and a four-digit number ( on the bottom) such that their ratio gives all digits from 1 to 9. For example, 134578/6729 =2 , 17496/5823=3 , 15768/3942=4, and so on. I have no idea how I can find the logic behind this question. Please help !! I've been searching for three days just to solve this question!

POSTED BY: serene H
6 Replies
Posted 3 days ago

A brute force method would be to look at all permutations of the 9 digits. You'd split each permutation, taking the first 5 to be the numerator and the last 4 to be the denominator. For each pair you'd check whether it creates an integer. 9! is 362880, so this will take a bit of time but probably not excessive.

allPerms = Permutations[Range@9];
Select[FromDigits /@ TakeDrop[#, 5] & /@ allPerms, IntegerQ@*Apply[Divide]]

This gives 187 possibilities. You can check to see whether the list is exhaustive, and you can filter it to just those that give integers less than 10.

Note: You can't get 1, so I assume that was an error in your question. Also, I assume that 134578/6729 should have been 13458/6729 and that 17496/5823 should have been 17469/5823.

POSTED BY: Eric Rimbey
Posted 3 days ago

Hello, I am trying to rewrite the code. I have this so far, but I have no idea how I can separate the list. It seems like the answer is right, but I will need to separate them. Please help!

Num5[ns_] := Sum[ ns[[i]] 10^(5-i), {i, 5} ]
Num4[ns_] := Sum[ ns[[i]] 10^(4-i), {i, 4} ]
Compute[ns_] := Mod[ Num5[ns[[1 ;; 5]]], Num4[ns[[6 ;; 9]]] ] == 0
Select[Permutations[Range[9]], Compute]
POSTED BY: serene H
Posted 3 days ago

What do you mean by "separate the list"?

POSTED BY: Eric Rimbey

What do you mean by "separate the list"?

Perhaps this?

candidates = Select[Permutations[Range[9]], Compute]
{FromDigits[#[[1 ;; 5]]], FromDigits[#[[6 ;;]]]} & /@ candidates // 
  AssociationMap[Divide[First@#, Last@#] &] // Sort

(*
<|{13458, 6729} -> 2, {13584, 6792} -> 2, {13854, 6927} -> 
  2, {14538, 7269} -> 2, {14586, 7293} -> 2, {14658, 7329} -> 
  2, {15384, 7692} -> 2, {15846, 7923} -> 2, {15864, 7932} -> 
  2, {18534, 9267} -> 2, {18546, 9273} -> 2, {18654, 9327} -> 
  2, {17469, 5823} -> 3, {17496, 5832} -> 3, {15768, 3942} -> 
  4, {17568, 4392} -> 4, {23184, 5796} -> 4, {31824, 7956} -> 
  4, {13485, 2697} -> 5, {13845, 2769} -> 5, {14685, 2937} -> 
  5, {14835, 2967} -> 5, {14865, 2973} -> 5, {16485, 3297} -> 
  5, {18645, 3729} -> 5, {31485, 6297} -> 5, {38145, 7629} -> 
  5, {46185, 9237} -> 5, {48135, 9627} -> 5, {48615, 9723} -> 
  5, {17658, 2943} -> 6, {27918, 4653} -> 6, {34182, 5697} -> 
  6, {16758, 2394} -> 7, {18459, 2637} -> 7, {31689, 4527} -> 
  7, {36918, 5274} -> 7, {37926, 5418} -> 7, {41832, 5976} -> 
  7, {53298, 7614} -> 7, {25496, 3187} -> 8, {36712, 4589} -> 
  8, {36728, 4591} -> 8, {37512, 4689} -> 8, {37528, 4691} -> 
  8, {38152, 4769} -> 8, {41896, 5237} -> 8, {42968, 5371} -> 
  8, {46312, 5789} -> 8, {46328, 5791} -> 8, {46712, 5839} -> 
  8, {47136, 5892} -> 8, {47328, 5916} -> 8, {47368, 5921} -> 
  8, {51832, 6479} -> 8, {53928, 6741} -> 8, {54312, 6789} -> 
  8, {54328, 6791} -> 8, {54712, 6839} -> 8, {56984, 7123} -> 
  8, {58496, 7312} -> 8, {58912, 7364} -> 8, {59328, 7416} -> 
  8, {59368, 7421} -> 8, {63152, 7894} -> 8, {63528, 7941} -> 
  8, {65392, 8174} -> 8, {65432, 8179} -> 8, {67152, 8394} -> 
  8, {67352, 8419} -> 8, {67512, 8439} -> 8, {71456, 8932} -> 
  8, {71536, 8942} -> 8, {71624, 8953} -> 8, {71632, 8954} -> 
  8, {73248, 9156} -> 8, {73264, 9158} -> 8, {73456, 9182} -> 
  8, {74528, 9316} -> 8, {74568, 9321} -> 8, {74816, 9352} -> 
  8, {75328, 9416} -> 8, {75368, 9421} -> 8, {76184, 9523} -> 
  8, {76248, 9531} -> 8, {76328, 9541} -> 8, {57429, 6381} -> 
  9, {58239, 6471} -> 9, {75249, 8361} -> 9, {45792, 3816} -> 
  12, {73548, 6129} -> 12, {89532, 7461} -> 12, {91584, 7632} -> 
  12, {67392, 5184} -> 13, {81549, 6273} -> 13, {94653, 7281} -> 
  13, {25746, 1839} -> 14, {27384, 1956} -> 14, {41538, 2967} -> 
  14, {46158, 3297} -> 14, {51492, 3678} -> 14, {54768, 3912} -> 
  14, {61572, 4398} -> 14, {65982, 4713} -> 14, {27945, 1863} -> 
  15, {92745, 6183} -> 15, {45936, 2871} -> 16, {73296, 4581} -> 
  16, {98352, 6147} -> 16, {26843, 1579} -> 17, {28543, 1679} -> 
  17, {29546, 1738} -> 17, {36958, 2174} -> 17, {45713, 2689} -> 
  17, {45781, 2693} -> 17, {54689, 3217} -> 17, {59126, 3478} -> 
  17, {64957, 3821} -> 17, {65297, 3841} -> 17, {67184, 3952} -> 
  17, {67218, 3954} -> 17, {76823, 4519} -> 17, {76891, 4523} -> 
  17, {78132, 4596} -> 17, {78523, 4619} -> 17, {78591, 4623} -> 
  17, {81532, 4796} -> 17, {83572, 4916} -> 17, {83657, 4921} -> 
  17, {89437, 5261} -> 17, {89471, 5263} -> 17, {89641, 5273} -> 
  17, {91426, 5378} -> 17, {92837, 5461} -> 17, {92871, 5463} -> 
  17, {93126, 5478} -> 17, {28674, 1593} -> 18, {51984, 2736} -> 
  19, {81567, 4293} -> 19, {51678, 2349} -> 22, {36294, 1578} -> 
  23, {81627, 3549} -> 23, {81972, 3564} -> 23, {39528, 1647} -> 
  24, {46872, 1953} -> 24, {42978, 1653} -> 26, {56498, 2173} -> 
  26, {61854, 2379} -> 26, {67314, 2589} -> 26, {67418, 2593} -> 
  26, {76518, 2943} -> 26, {82654, 3179} -> 26, {89726, 3451} -> 
  26, {92846, 3571} -> 26, {39852, 1476} -> 27, {49572, 1836} -> 
  27, {69741, 2583} -> 27, {96714, 3582} -> 27, {75348, 2691} -> 
  28, {37584, 1296} -> 29, {73689, 2541} -> 29, {75168, 2349} -> 
  32, {48265, 1379} -> 35, {63945, 1827} -> 35, {64295, 1837} -> 
  35, {74865, 2139} -> 35, {93485, 2671} -> 35, {65934, 1782} -> 
  37, {65892, 1734} -> 38, {74328, 1956} -> 38, {93654, 2178} -> 
  43, {58476, 1329} -> 44, {59268, 1347} -> 44, {67892, 1543} -> 
  44, {69432, 1578} -> 44, {95348, 2167} -> 44, {58374, 1269} -> 
  46, {95472, 1836} -> 52, {65879, 1243} -> 53, {75896, 1432} -> 
  53, {84376, 1592} -> 53, {92538, 1746} -> 53, {73986, 1254} -> 
  59, {79546, 1283} -> 62, {94736, 1528} -> 62, {83754, 1269} -> 
  66, {98736, 1452} -> 68|>
*)
POSTED BY: Rohit Namjoshi

Instead of Num4 and Num5, you can use FromDigits, which will work for any number of digits.

FromDigits@{1, 3, 4, 5}
(* 1345 *)

FromDigits@{1, 3, 4, 5, 8, 6, 7, 2, 9}
(* 134586729 *)
POSTED BY: Rohit Namjoshi

Crossposted here.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract