New answers tagged

1

Just as an idea (though Permutations and Select preserved): ({p2, p3} = DeleteCases[Permutations[Range[0, 9], {#}], {0, __}] & /@ {2, 3}; list = Flatten[Outer[Join, p3, p2, 1], 1]; tmp2 = (FromDigits[#] & /@ list[[All, 1 ;; 3]])*(FromDigits[#] & /@ list[[All, 4 ;; 5]]); list2 = IntegerDigits /@ tmp2; res = Select[DeleteDuplicates /@ ...


0

Solution Here is a way of efficiently generating tuples of objects that satisfy a given criterion (criterion, which takes a list of objects and returns True or False). We consider the general case of creating tuples from several lists of objects. This works best if the criterion can be applied before reaching the desired tuple length. (e.g. if I want to ...


0

This did turn out somewhat clumsy, but it avoids the n! complexity of Permutations f[c_, {}] = c; f[c_, s_] := g[c, First[s], Rest[s]] g[c_, a_, b_] := Sequence @@ MapIndexed[f[Append[c, {a, #}], Delete[b, First[#2]]] &, b] h[L_] := {f[{}, L]} h[{a, b, c, d, e, F}] (*{{{a, b}, {c, d}, {e, F}}, {{a, b}, {c, e}, {d, F}}, {{a, b}, {c, F}, {d, e}}, {{a, ...


6

You can also use FindHamiltonianCycle. To convert Hamiltonian path problem to Hamiltonian cycle problem, just add one vertex and connect it to all other vertices. After that just run FindHamiltonianCycle[g, All] For example, countHamiltonianPaths[g_] := Length[ FindHamiltonianCycle[ AdjacencyGraph[PadRight[AdjacencyMatrix[g], (VertexCount[g] + ...


2

This is the same basic algorithm but in my own style. It runs only about twice as fast as yours, but hopefully it will be of interest anyway. The recursive function count maintains the current vertex, the adjacency list $a$ and a counter $i$. At each call the current vertex is removed from the adjacency list and the function recursively scanned over the ...


4

This is not really very efficient but here goes. We can create a rational function that is effectively a generating function in three variables, one to force 8 factors, one to force a sum e1ual to 24, one to force a sum of squares equal to 86. The other parameters just keep track of what factors get used in a coefficient. vals = Range[5]; aa = Array[a, 5]; ...


8

A slightly different approach with Reduce (or Solve) We define: matC = Range[5]; (* the list of the integers from which we build a list *) Let us use a vector to indicate the multiplicities of any integer: matX = Array[ x, 5 ]; (* @Kuba: that's more concise, indeed *) So x[1] will tell us how many times $1$ appears a possible solution. We can then ...


13

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] {{5, 5, 4, 2, 2, 2, 2, 2}, {5, 5, 3, 3, 3, 2, 2, 1}, {5, 4, 4, 4, 2, 2, 2, 1}, {5, 4, 4, 3, 3, 3, 1, 1}, {4, 4, 4, 4, 4, 2, 1, 1}} Slightly more general approach (in case where IntegerPartitions is not what we need): ClearAll[ar, a]; ar = Array[a, 8] ar /. Solve[Flatten@{ ...



Top 50 recent answers are included