2
$\begingroup$

I have a simple table that I can't quite make work. Columns are as follows

  1. A function returning 1 if x divides 12 and 0 else
  2. A function returning 1 if x divides 18 and 0 else
  3. Columns 1 and 2 multiplied together to produce 1 if x divides both 12 and 18, and 0 else
  4. Column 3s individual entries multiplied by the number of 1s in column 3, returning 4 if x divides both 12 and 18, and 0 else
  5. An accumulating sum of column 4, which should therefore read {4,8,12,12,12,16,16,16,16,16}

I have problems with columns 4 and 5. In the table below, I have had to manually enter the count for column 4 (Count just returns 0), and the sum doesn't work in column 5 (the sum drops back to 0 whenever column 4 equals 0, and I have no idea how it has managed to add together four 4s to get 24).

How do I fix this?

TableForm[ Table[{If[MemberQ[Divisors[12], x], 1, 0], If[MemberQ[Divisors[18], x], 1, 0], If[MemberQ[Divisors[12], x], 1, 0]* If[MemberQ[Divisors[18], x], 1, 0], 4*If[MemberQ[Divisors[12], x], 1, 0]* If[MemberQ[Divisors[18], x], 1, 0], Sum[ 4*If[MemberQ[Divisors[12], x], 1, 0]* If[MemberQ[Divisors[18], x], 1, 0], {i, 1, x}]}, {x, 1, 10}], TableHeadings -> {{"x=1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}, {"D[12]", "D[18]", "D[12]D[18]", "(Count[D[12]D[18]])(D[12]D[18])", "Sum[Count[D[12]D[18]])(D[12]D[18])]"}}, TableAlignments -> Center] 
$\endgroup$
1
  • $\begingroup$ For #5, you've got x where you should have i, if you want to accumulate: E.g. Sum[4*Boole[Divisible[12, i]]*Boole[Divisible[18, i]], {i, 1, x}] $\endgroup$ Commented Mar 11, 2019 at 16:38

2 Answers 2

3
$\begingroup$

Perhaps this, if I got the description of column 4 right:

With[{c1 = Boole[Divisible[12, Range@10]], c2 = Boole[Divisible[18, Range@10]]}, {c3 = c1*c2}, {c4 = Total[c3] c3}, Transpose@{c1, c2, c3, c4, Accumulate[c4]} ] // TableForm 

Mathematica graphics

$\endgroup$
0
$\begingroup$

Similarly,

With[{t = Table[ With[{result = Boole@Divisible[{12, 18}, i]}, {i, Sequence @@ result, Sequence @@ ({1, 4} Times @@ result)} ], {i, 12} ]}, Transpose@Join[Transpose@t, {Accumulate[t[[All, 5]]]}] // TableForm ] 

Mathematica graphics

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.