1
$\begingroup$

I want to create a table of random numbers and their divisors. The random numbers are given by

TableForm[{{Subscript[m, 1] = RandomInteger[{2, 50}], 
 Subscript[n, 1] = RandomInteger[{2, 50}]}, 
 {Subscript[m, 2] = RandomInteger[{2, 50}], 
 Subscript[n, 2] = RandomInteger[{2, 50}]}, 
 {Subscript[m, 3] = RandomInteger[{2, 50}], 
 Subscript[n, 3] = RandomInteger[{2, 50}]}}, 
 TableHeadings -> {{"1", "2", "3"}, {"m", "n"}}, 
 TableAlignments -> Center]

So far so good. A neat table with delimiters for the headings. But when I try to add the divisors it all goes wrong:

TableForm[{{Subscript[m, 1] = RandomInteger[{2, 50}], 
 Subscript[n, 1] = RandomInteger[{2, 50}], 
   Divisors[Subscript[m, 1]], 
   Divisors[Subscript[n, 1]]}, {Subscript[m, 2] = 
RandomInteger[{2, 50}], 
   Subscript[n, 2] = RandomInteger[{2, 50}], 
   Divisors[Subscript[m, 2]], Divisors[Subscript[n, 2]]}, 
 {Subscript[m, 3] = RandomInteger[{2, 50}], 
  Subscript[n, 3] = RandomInteger[{2, 50}], 
   Divisors[Subscript[m, 3]], Divisors[Subscript[n, 3]]}}, 
TableHeadings -> {{"1", "2", "3"}, {"m", "n", "divisors[m]", 
"divisors[n]"}}, 
TableAlignments -> Center]

I want the heading delimiters to remain, but they disappear; and I'd like the divisors to appear in a comma-delimited horizontal list (for example Divisors[4] would produce 1,2,4 rather than a vertical list.

$\endgroup$
2
$\begingroup$
SeedRandom[1]
table = RandomInteger[{2, 50}, {3, 2}];
TableForm[Join[table, Map[Row[Divisors[#], " "] &, table, {-1}], 2], 
 TableHeadings -> {{"1", "2", "3"}, {"m", "n", "divisors[m]", "divisors[n]"}}, 
 TableAlignments -> Center]

enter image description here

$\endgroup$
  • $\begingroup$ Many thanks @kglr. I'm struggling to incorporate this solution into other forms of table (mainly because I don't understand what you did!) I'd also like to create a table that lists n from 1 to 25 in one column, and Divisors[n] in the second column, formatted as you did in your answer above. $\endgroup$ – Richard Burke-Ward Mar 8 at 11:10
  • $\begingroup$ @Richard, table is a 3X2 matrix of numbers as in your first example. Map[Divisors, table, {-1}] gives the divisors of each entry in that matrix. I wrapped each list of divisors with Row[..., " "] so that each divisor list is displayed horizontally. A simpler form using the options TableDirections is TableForm[Join[table, Map[Divisors, table, {-1}], 2], TableHeadings -> {{"1", "2", "3"}, {"m", "n", "divisors[m]", "divisors[n]"}, None}, TableAlignments -> Center, TableDirections -> {Column, Row, Row}] but, due to a glitch/bug it doesn't display the dividers. $\endgroup$ – kglr Mar 8 at 11:23
  • $\begingroup$ ... so i had to use Row around the inner lists. The example in your comment can be done using TableForm[Transpose[{Range[25], Row[Divisors[#]] & /@ Range[25]}], TableHeadings -> {Automatic, {"n", "divisors[n]"}}, TableAlignments -> Center]. $\endgroup$ – kglr Mar 8 at 11:24

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.