28
votes
Accepted
Faster derangements?
Chunks of derangements
Since I've already written library link code generating permutations, generating derangements requires just few tweaks:
...
25
votes
Accepted
25
votes
23
votes
Accepted
How to generate all Feynman diagrams with Mathematica?
Here is a piece of code that is inspired by quantum field theory. The physics background can be found in this physics.SE post.
First, we define some auxiliary functions:
...
21
votes
Accepted
20
votes
House of Santa Claus
This is of course the Chinese postman problem, which is solved by the function FindPostmanTour[]. First, represent the edges of the directed graph:
...
20
votes
Transform a number to a factorial
An example target, a bit over half-a-million digits:
x = 123456!;
IntegerLength@x
574965
Results for target, and a non-hit:
...
19
votes
Accepted
Lazy form of Tuples/Outer to loop over list of lists
The implementation of lazy tuples here pretty much contains the solution to the lazy Outer problem. I will take the relevant parts from that code.
The following ...
18
votes
Faster derangements?
Here is one way to generate them directly: it is based on a way to generate all permutations but discards invalid ones early:
...
17
votes
Accepted
Generating Tuples with restrictions
Select[
IntegerPartitions[24, {8}, Range[5]],
#.# == 86 &
]
...

Kuba♦
- 136k
17
votes
Accepted
How to generate all possible orderless partitions of a list according to another list?
A solution using Repeated, ReplaceList, and the Orderless attribute.
...
17
votes
Permutations of nested parentheses (Dyck words)
StringReplaceList
I just realized that there is a comparatively clean though not highly efficient way to write this using ...
16
votes
How to count all cliques (not just maximal ones) in graphs?
Mathematica only finds maximal cliques, i.e. cliques (complete subgraphs) that are not part of a larger clique.
Computing the number of all cliques given the maximal ones is not trivial because some ...
16
votes
Accepted
16
votes
Accepted
Mathematica implementation of Zeilberger's algorithm (previously done in Maple)
There is also a newer package, HolonomicFunctions, that has an implementation of Chyzak's generalization of Zeilberger's algorithm. To perform the desired task, use the following commands:
...
15
votes
How to generate all possible orderless partitions of a list according to another list?
Permutations treats repeated elements as identical, so you can get a flattened version of the desired result with something like
...
15
votes
Accepted
List all possible license plate numbers
I would use
a = Alphabet[]; (* letter *)
d = Range[0, 9]; (* digit *)
result = Tuples[{a, a, d, d, d, d}];
15
votes
Transform a number to a factorial
Stirling's approximation, $n! \sim n^ne^{-n}$, is the Swiss army knife of factorial problems. If $x = n!$, this implies that $\ln x \sim n \ln n - n$; and surprisingly this can be solved with a ...
15
votes
Can you give a faster implementation with Mathematica for these q-analog functions?
There are lots of QFunctions (see also wikipedia)
Some are the q(,t)-analog version of the original MATH objects. (orthogonal polynomials, special functions, etc. ) However, it seems to me that some ...
14
votes
Lazy form of Tuples/Outer to loop over list of lists
Overview
Here is a refinement of @Leonid's approach that is a bit faster. The basic idea is to create a TuplesFunction that encapsulates the tuples information, ...
14
votes
Solving word search puzzles
Update
To allow string with multiple words separated by single space, and possible multiple instances of word and non empty intersection of positions:
...
14
votes
Permutations of lists of fixed even numbers
The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage.
...
14
votes
Accepted
Permutations of lists of fixed even numbers
Permutations where no element remains in its original place are called derangements. Counting them is easy enough: the number of derangements of a set of size $n$ is $!n$, or the subfactorial of $n$. ...
14
votes
Accepted
Generating set partition diagrams
I need to reproduce this diagram ...
We can use the function blobF from this answer to generate blobs around subsets:
...
14
votes
All possible combinations of ways to write an equation
Tuples + Dot:
Tuples[{1, -1}, 3].{q, p1, p2}
{p1 + p2 + q, p1 - p2 + q, -p1 + p2 + q, -...
13
votes
Accepted
Tuples optimization challenge
Don't use brute force, instead compute probabilities for each amount of leading zero bits in OrderDistribution for the smallest entry of ...
13
votes
Accepted
How can I correctly use LazySubsets from Wolfram's Lazy package?
The package doesn't seem to be well-documented, so I have to guess. If I guess it right, those Lazy* functions are supposed to work only on ...
12
votes
Tuples with one "joker" digit?
Permutations can do this. You just need to give it enough copies of 1 and 2 such that they ...
12
votes
Accepted
Cyclic and Non-cyclic Permutations
Per the request, I post my comment as an answer:
First question
cy := Permute[#, CyclicGroup[Length@#]] &
cy[Range@5]
{{1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {3, 4,...
12
votes
Any alternative way to compute IntegerPartitions?
There are 190,569,292 unrestricted integer partitions of 100 (PartitionsP@100).
This will need >1gb of RAM just to keep the final result.
You can generate them in ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
combinatorics × 513list-manipulation × 134
permutation × 79
graphs-and-networks × 78
performance-tuning × 49
partitions × 47
programming × 22
combinatorica × 22
matrix × 20
number-theory × 18
probability-or-statistics × 17
algorithm × 17
summation × 16
sets × 16
function-construction × 15
functions × 14
group-theory × 13
pattern-matching × 11
subset × 11
equation-solving × 9
memory × 9
lazy-computations × 9
mathematical-optimization × 8
special-functions × 8
string-manipulation × 7