0
$\begingroup$

enter image description here

I want to generate this exact list

I have this code and it works perfectly however I want to do it in a different way without using these functions, can someone please help me do this?

x = 180 (p - 2 q)/p


DeleteCases[
 Flatten[
  Table[
   If[
    IntegerQ[x],
    If[x > 0,
     If[GCD[p, q] == 1,
      {p, q, x}
      ]
     ]
    ],
   {p, 3, 40, 1},
   {q, 1, p/2}],
  1
  ],
 Null
 ]

What is another solution to this problem?

$\endgroup$
2
  • $\begingroup$ Have you seen the examples regarding star polygons in the Applications section of the documentation for Polygon? $\endgroup$
    – MarcoB
    Apr 4, 2019 at 3:08
  • $\begingroup$ Can you explain why did you rewrite the question which was already answered? $\endgroup$
    – Kuba
    Apr 4, 2019 at 6:35

3 Answers 3

4
+100
$\begingroup$

You could use the following, with your definition of x:

Select[
  Table[{p, q, x}, {p, 3, 40}, {q, 1, p/2}] ~ Flatten ~ 1,
  Apply[IntegerQ[#3] && #3 > 0 && GCD[#1, #2] == 1 &]
]
$\endgroup$
5
$\begingroup$

I use the Table code from your second code block and strip out the Graphics directives since I think that ArrayPlot is actually the most efficient way to plot this.

mytable = Table[
   Table[
    If[EvenQ[Binomial[col, k]], 1, 0], {col, 0, n, 1}, {k, 0, col, 
     1}], {n, {7, 15, 31, 63, 127, 255}}];

This is going to result in $mytable$ having 6 different ragged matrices (that is the first row will only have a single number, the second row 2 numbers, etc.). We can easily plot them with:

ArrayPlot /@ PadLeft /@ mytable

The /@ notation basically means "iterate over all of the parts of whatever comes after" and is sometimes a nice substitute for Table. So PadLeft/@mytable is going to create rectangular matrices by padding the left side of each row with zeros, then we iterate over all of those padded matrices with ArrayPlot. This gets us:

Sierpinski meshes

Which doesn't have the exact same colouring as yours, but it acts as a quick check to make sure they look the same. If you decided you wanted to use ArrayPlot yourself, there are options for colouring it however you like.

tally = Tally /@ Flatten /@ mytable
fractions = N @ #[[2, 2]]/Total[#[[All, 2]]] & /@ tally

Now I use the Tally function on the original unpadded list. I have to Flatten it first since I want to see how many 0s and 1s there are, I don't want it to tell me how many of each list of 0s and 1s there are. You can try Tally /@ mytable to see the difference. The N in the second line tells it to return decimal numbers (0.25) rather than exact numbers (1/4). The number sign is a Slot which is basically just a placeholder. I'm dividing the second-row second-column value of each list that I put into $tally$. The first list in $tally$ looks like: {{0, 27}, {1, 9}} telling us there are 27 zeroes and 9 ones. So all the second row does is go 9/(9+27) to get us 1/4.

You can now easily combine this with the list of n values:

{{7, 15, 31, 63, 127, 255}, fractions*100}\[Transpose]//TableForm

I have to multiply the fractions by 100 to get percentages, and the \[Transpose] is there to put them into nice columns. The //TableForm takes the whole thing and turns it into a nice table.

$\endgroup$
2
$\begingroup$
Join @@ Table[
  Table[
   {p, q, 180 (1 - 2 q/p)},
   {q, Select[Range[1, Ceiling[p/2] - 1], CoprimeQ[p, #] &]}],
  {p, Select[Divisors[360], Between[{3, 40}]]}
  ]
$\endgroup$
4
  • $\begingroup$ It really would be nicer were you to make your answers self-contained w.r.t. evaluation. $\endgroup$
    – m_goldberg
    Apr 4, 2019 at 5:57
  • $\begingroup$ @m_goldberg I don't understand. What is the matter? This is supposed to be self-contained. Well, I'll check when I am back at my keyboard. $\endgroup$ Apr 4, 2019 at 12:39
  • $\begingroup$ I apologize. It is self-contained. I didn't read it carefully enough. Also, thrown off because you didn't show results. $\endgroup$
    – m_goldberg
    Apr 4, 2019 at 14:28
  • $\begingroup$ @m_goldberg No problem. The output was a bit lengthy and also already known, so I decided to leave it away. Anyways, I am going to take more care for self-containedness in the future; that won't harm. $\endgroup$ Apr 4, 2019 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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