The challenge is simple: Reorder the first integers {0, 1, 2, ..., n} into an ordered list so that the following three conditions are met:
If k is the last element in the list, then all of its prime factors divide its immediate predecessor.
If a term k lies between two other terms of the list and p is a prime factor of k, then p divides either the predecessor of k or the successor of k, but not both.
For n = 0, the solution is by convention (0). You can ignore this edge case as we exclude it from the requirement for simplicity.
- The list is the lexicographically earliest list among all lists whose terms satisfy conditions (1) and (2).
The first few permutations of {0,1,...,n} that meet these conditions are:
0 -> (0)
1 -> (0, 1)
2 -> (0, 2, 1)
3 -> (1, 2, 0, 3)
4 -> (0, 3, 1, 2, 4)
5 -> (1, 2, 4, 3, 0, 5)
6 -> (1, 2, 0, 5, 3, 6, 4)
7 -> (2, 1, 3, 6, 4, 5, 0, 7)
8 -> (1, 2, 4, 3, 6, 8, 5, 0, 7)
9 -> (3, 1, 2, 4, 5, 0, 7, 8, 6, 9)
0 and 1 have no prime factors. Recall that all numbers divide 0.
(If this last remark surprises you, read it on page 14 of Apostol's Introduction to Analytic Number Theory.)
Hint: this is a finite variant of the OEIS-sequence A280864 by Rémy Sigrist.
Summary:
Input: A positive integer n. (We exclude the edge case n=0 for simplicity.)
Output: A permutation of {0,1,...,n} subject to the conditions (1), (2) and (3).
This is code-golf, so each language's shortest code in bytes wins.
0
as it's an exceptional edge case. \$\endgroup\$