I am trying to write a function that is supposed to return a list of all complementary k-tuples of integers consisting of only 0's and 1's such that every tuple is not coprime setwise (i.e. GCD of all k integers is > 1). The integers are considered integers in base 10, despite having only 2 digits.
My original version of the code that returns the correct result looks like this:
Clear[NonCoprimeList];
NonCoprimeList[n_, m_] := NonCoprimeList[n, m] =
If[n == 0, {{}}, If[m == 1, {{FromDigits[IntegerDigits[2^n - 1, 2]]}},
FullList = Flatten[Table[Table[
Prepend[FromDigits /@
IntegerDigits[2^(Flatten[Position[Reverse[IntegerDigits[i, 2]], 0]] - 1).# &
/@ (Reverse[IntegerDigits[#, 10, Count[IntegerDigits[i, 2], 0]]] & /@ #[[j]]), 2],
FromDigits[IntegerDigits[i, 2]]], {j, Length[#]}] &[
NonCoprimeList[Count[IntegerDigits[i, 2], 0], m - 1]], {i, 2^(n - 1), 2^n - 2}], 1]]]
T[n_, k_] := Length[#] &[Select[GCD @@ # > 1 &][NonCoprimeList[n, k]]];
T[8, 3]
The output of this code is T[8, 3] = 98.
Instead of applying Select[GCD @@ # > 1 &][...] at the end to the whole array, I want to place it inside the function definition and use the fact that k integers cannot have GCD > 1 if k - 1 are already setwise coprime. So I am changing the code as follows:
Clear[NonCoprimeList];
NonCoprimeList[n_, m_] := NonCoprimeList[n, m] =
If[n == 0, {{}}, If[m == 1, {{FromDigits[IntegerDigits[2^n - 1, 2]]}},
FullList = Select[GCD @@ # > 1 &][Flatten[Table[Table[
Prepend[FromDigits /@
IntegerDigits[2^(Flatten[Position[Reverse[IntegerDigits[i, 2]], 0]] - 1).# &
/@ (Reverse[IntegerDigits[#, 10, Count[IntegerDigits[i, 2], 0]]] & /@ #[[j]]), 2],
FromDigits[IntegerDigits[i, 2]]], {j, Length[#]}] &[
NonCoprimeList[Count[IntegerDigits[i, 2], 0], m - 1]], {i, 2^(n - 1), 2^n - 2}], 1]]]]
T[n_, k_] := Length[NonCoprimeList[n, k]];
T[8, 3]
Suddenly, it now returns a different result, T[8, 3] = 84.
I spent the whole day trying to figure out why these two codes are not equivalent. I found the triples that the second code "misses" for some reason, e.g. {10100000, 1011010, 101}. This must be included because GCD[10100000, 1011010, 101] == 101.
Can anyone please help me understand what is going on?
Or maybe you can suggest a better way to define this function?
NonCoprimeList[4, 2]gives me{{1001, 110}, {1010, 101}, {1100, 11}}. Why should that not include{1111,11}? $\endgroup$nis meant as a sort of maximum to the search (i.e. you'll only search up to1111whennis4, for example)? And the parametermis the size of the non-coprime sets you're looking for, correct? $\endgroup$1across the whole set is constrained by the argumentn? $\endgroup$