3
$\begingroup$

first let me show what I have working correctly

f = Permutations[Range[5], {3}]
Riffle[f, Apply[CoprimeQ, f, {1}]]

now I would like to automate the deletion of a list {a,b,c} that returns false from this test. I can probably do it with Do[] but I wanted to learn functional programming the right way using built in functions and pure function on one line of code.

I had spent a lot of time trying to follow this example for DeleteCases[] here but it seems that it requires building a custom function. it also does not perfectly correlate to my example because in that tutorial they are comparing two lists. I tried to understand it but it went over my head.

here is not working example #1

f = Permutations[Range[5], {3}]
DeleteCases[f, CoprimeQ]

here is not working example #2

f = Permutations[Range[5], {3}]
DeleteCases[f, (CoprimeQ[_x] == False) -> x]

also I prefer not to use shorthand @@ @/ @@@. you can see I use the full form of Apply[] which is preferable to me while I am still learning. I can convert between them if I need to so post what you have.

$\endgroup$
6
$\begingroup$
DeleteCases[{x__ /; Not@CoprimeQ[x]}]@f

DeleteCases[Except[_List?(Apply[CoprimeQ])]]@f

both give

{{1, 2, 3}, {1, 2, 5}, {1, 3, 2}, {1, 3, 4}, {1, 3, 5}, {1, 4, 3}, {1, 4, 5},
{1, 5, 2}, {1, 5, 3}, {1, 5, 4}, {2, 1, 3}, {2, 1, 5}, {2, 3, 1}, {2, 3, 5}, 
{2, 5, 1}, {2, 5, 3},  {3, 1, 2}, {3, 1, 4}, {3, 1, 5}, {3, 2, 1}, {3, 2, 5}, 
{3, 4, 1}, {3, 4, 5}, {3, 5, 1},  {3, 5, 2}, {3, 5, 4}, {4, 1, 3}, {4, 1, 5}, 
{4, 3, 1}, {4, 3, 5}, {4, 5, 1}, {4, 5, 3}, {5, 1, 2}, {5, 1, 3}, {5, 1, 4}, 
{5, 2, 1}, {5, 2, 3}, {5, 3, 1}, {5, 3, 2}, {5, 3, 4}, {5, 4, 1}, {5, 4, 3}}

You an also use

Cases[{x__ /; CoprimeQ[x]}]@f

Cases[_List?(Apply[CoprimeQ])]@f

Select[Apply[CoprimeQ]]@f

Pick[f, Apply[CoprimeQ, f, 2]]
| improve this answer | |
$\endgroup$
0
$\begingroup$

after reading and testing the excellent examples provided by kglr, I found a solution based on this answer that is better suited to my preferred choice of syntax style.

Select[Subsets[Range[5], {3}], Apply[CoprimeQ]]

I was making multiple mistakes. I was using Permutations instead of Subsets. it was difficult to find documentation on @ but I found it here. not exactly the best wolfram documentation for more than one argument but it got me through my misunderstanding of the example posted here. this was adapted from

f = Permutations[Range[5], {3}]
Select[Apply[CoprimeQ]]@f

notice all the @ /@ ? __/; is gone after I reworked it for my needs.

after I consolidated everything to a single line of code using pure functions, I was then able to make a Table. example:

Table[
Length[Select[Subsets[Range[x], {3}], Apply[CoprimeQ]]], {x, 3, 100}
]

which gives

{1, 2, 7, 8, 19, 25, 37, 42, 73, 79, 124, 138, 159, 183, 262, 277,
378, 405, 454, 491, 640, 668, 794, 850, 959, 1016, 1257, 1285, 1562,
1668, 1805, 1905, 2088, 2150, 2545, 2673, 2866, 2968, 3457, 3522,
4063, 4228, 4431, 4620, 5269, 5385, 5936, 6106, 6453, 6684, 7513,
7660, 8166, 8400, 8834, 9141, 10168, 10287, 11388, 11752, 12202,
12614, 13341, 13521, 14848, 15263, 15921, 16164, 17657, 17918, 19505,
20031, 20603, 21125, 22248, 22509, 24364, 24799, 25796, 26434, 28493,
28761, 30057, 30766, 31830, 32476, 34843, 35115, 36728, 37513, 38734,
39577, 41208, 41665, 44470, 45192, 46424, 47086}

I then found a match in OEIS. with code example

a[n_] := Select[Subsets[Range[n], {3}], And @@ (GCD @@ # == 1 & /@ Subsets[#, {2}]) &] // Length;
a /@ Range[49]

you can decide who has the cleanest code out of all the examples. big thanks to kglr for helping me through when I was stuck.

| improve this answer | |
$\endgroup$

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.