Skip to main content
28 votes
Accepted

Faster derangements?

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

Faster derangements?

This is the fastest method I have come up with: ...
Mr.Wizard's user avatar
  • 272k
25 votes
Accepted

Solving word search puzzles

Here we go... ...
C. E.'s user avatar
  • 70.7k
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: ...
AccidentalFourierTransform's user avatar
21 votes
Accepted

Programming a bishop's move on a grid

...
kglr's user avatar
  • 396k
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: ...
J. M.'s missing motivation's 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: ...
ciao's user avatar
  • 25.8k
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 ...
Leonid Shifrin's user avatar
19 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: ...
Martin Ender's user avatar
  • 8,774
18 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: ...
kglr's user avatar
  • 396k
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. ...
Mr.Wizard's user avatar
  • 272k
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 ...
Mr.Wizard's user avatar
  • 272k
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: ...
Christoph Koutschan's 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 ...
Simon Woods's user avatar
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}];
Szabolcs's user avatar
  • 235k
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 ...
Michael Seifert's user avatar
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 ...
138 Aspen's user avatar
  • 1,373
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, ...
Carl Woll's user avatar
  • 131k
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: ...
ubpdqn's user avatar
  • 61.6k
14 votes

Permutations of lists of fixed even numbers

The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage. ...
vapor's user avatar
  • 7,921
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$. ...
Martin Ender's user avatar
  • 8,774
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, -...
kglr's user avatar
  • 396k
14 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 ...
xzczd's user avatar
  • 66.8k
13 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,...
yode's user avatar
  • 26.8k
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 ...
kirma's user avatar
  • 19.1k
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 ...
Martin Ender's user avatar
  • 8,774
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 ...
ciao's user avatar
  • 25.8k
12 votes

How to generate all possible orderless partitions of a list according to another list?

It's far from pretty, using pattern matching (OrderlessPatternSequence): ...
kirma's user avatar
  • 19.1k
12 votes

How to generate all possible orderless partitions of a list according to another list?

...
BoLe's user avatar
  • 5,859
12 votes

Faster derangements?

Here is a straightforward compiled implementation of Knuth's "Algorithm X" for lexicographically generating restricted permutations, specialized to the derangement case: ...
J. M.'s missing motivation's user avatar

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