30 votes
Accepted

Efficient lazy weak compositions

Chunks of weak compositions Here is slightly modified version of algorithm used in Combinatorica`NextComposition converted to a ...
user avatar
  • 14.7k
28 votes
Accepted

Faster derangements?

Chunks of derangements Since I've already written library link code generating permutations, generating derangements requires just few tweaks: ...
user avatar
  • 14.7k
25 votes

Permutations[Range[12]] produces an error instead of a list

Since Mathematica 8 it is possible generate the elements of any group one by one with GroupElements. Here's for example a randomly chosen element of the permutation ...
user avatar
  • 5,921
25 votes
Accepted

Solving word search puzzles

Here we go... ...
user avatar
  • 68.8k
25 votes

Faster derangements?

This is the fastest method I have come up with: ...
user avatar
  • 264k
21 votes
Accepted

Programming a bishop's move on a grid

...
user avatar
  • 349k
20 votes

Alternative to Subsets to generate k-combinations

Subsets function takes optional third argument with standard sequence specification. Using this third argument you can take subsets "in chunks". For example, ...
user avatar
  • 14.7k
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: ...
user avatar
20 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: ...
user avatar
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: ...
user avatar
  • 25.1k
18 votes

Improving speed of code computing number of nonrepeating partitions

Here is a summary of comments (before @ciao's best answer above), with a change in notation. These functions calculate the number of partitions of n into exactly <...
user avatar
  • 14.9k
18 votes
Accepted

Improving speed of code computing number of nonrepeating partitions

This seems pretty quick, particularly on larger cases / larger k, e.g. 451, 29, 101 finishes in a few seconds on the loungebook. N.B. - I have not tested this ...
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: ...
user avatar
  • 8,564
17 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 ...
user avatar
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. ...
user avatar
  • 264k
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 ...
user avatar
  • 264k
16 votes

Improving speed of code computing number of nonrepeating partitions

Here is a totally different approach based on the fact that successive products forming the generating function are due to multiplication by a binomial $1+t*z^j$. Form a matrix $v$ of zeros with $n+1$ ...
user avatar
  • 14.9k
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 ...
user avatar
  • 225k
16 votes
Accepted

Random Partitions

...
user avatar
16 votes
Accepted

Generating Tuples with restrictions

Select[ IntegerPartitions[24, {8}, Range[5]], #.# == 86 & ] ...
user avatar
  • 132k
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: ...
user avatar
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 ...
user avatar
  • 83.4k
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}];
user avatar
  • 225k
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 ...
user avatar
14 votes
Accepted

Finding all sublists or substrings of a given list/string

TMTOWTDI applies to both of these problems. Below I present an overview of various approaches I've come across, followed by timing data obtained in 10.4 on Windows 10 (the timing code is available as ...
user avatar
  • 8,564
14 votes

generating integer partitions

I needed to do this sometime ago while investigating Bell polynomial analogs. Normally, you'd do FrobeniusSolve[Range[n], n] but the fastest variation (and quite ...
user avatar
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: ...
user avatar
  • 55.1k
14 votes

Permutations of lists of fixed even numbers

The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage. ...
user avatar
  • 7,661
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$. ...
user avatar
  • 8,564
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: ...
user avatar
  • 349k

Only top scored, non community-wiki answers of a minimum length are eligible