# Description

Let a permutation of the integers {1, 2, ..., n} be called minimally interpolable if no set of k+2 points (together with their indices) fall on a polynomial of degree k. That is,

1. No two points fall on a horizontal line (0-degree polynomial)
2. No three points fall on a line (1-degree polynomial)
3. No four points fall on a parabola (2-degree polynomial)
4. Et cetera.

# Challenge

Write a program that computes OEIS sequence A301802(n), the number of minimally interpolable permutations of {1, 2, ..., n} for n as a large as possible.

# Scoring

I will time your code on my computer (2.3 GHz Intel Core i5, 8 GB RAM) with increasing inputs. Your score will be the greatest input that takes less than 1 minute to output the correct value.

# Example

For example, the permutation [1, 2, 4, 3] is minimally interpolable because

the terms together with their indices
[(1, 1), (2, 2), (3, 4), (4, 3)]
have the property that
(0) No two points have the same y-value.
(1) No three points lie on a line.
(2) No four points lie on a parabola.


In the illustration, you can see that the horizontal lines (red) have at most one point on them, the lines (blue) have at most two points on them, and the parabolas (green) have three points on them.

# Data

Here are the minimally interpolable permutations for n=3, n=4, and n=5:

n = 3: [1,3,2],[2,1,3],[2,3,1],[3,1,2]
n = 4: [1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,1,4,3],[2,3,1,4],[2,4,1,3],[2,4,3,1],[3,1,2,4],[3,1,4,2],[3,2,4,1],[3,4,1,2],[3,4,2,1],[4,1,3,2],[4,2,1,3],[4,2,3,1],[4,3,1,2]
n = 5: [1,2,5,3,4],[1,3,2,5,4],[1,3,4,2,5],[1,4,2,3,5],[1,4,3,5,2],[1,4,5,2,3],[1,4,5,3,2],[1,5,3,2,4],[2,1,4,3,5],[2,3,1,4,5],[2,3,5,1,4],[2,3,5,4,1],[2,4,1,5,3],[2,4,3,1,5],[2,4,5,1,3],[2,5,1,3,4],[2,5,1,4,3],[2,5,3,4,1],[2,5,4,1,3],[3,1,4,5,2],[3,1,5,2,4],[3,1,5,4,2],[3,2,5,1,4],[3,2,5,4,1],[3,4,1,2,5],[3,4,1,5,2],[3,5,1,2,4],[3,5,1,4,2],[3,5,2,1,4],[4,1,2,5,3],[4,1,3,2,5],[4,1,5,2,3],[4,1,5,3,2],[4,2,1,5,3],[4,2,3,5,1],[4,2,5,1,3],[4,3,1,2,5],[4,3,1,5,2],[4,3,5,2,1],[4,5,2,3,1],[5,1,3,4,2],[5,2,1,3,4],[5,2,1,4,3],[5,2,3,1,4],[5,2,4,3,1],[5,3,2,4,1],[5,3,4,1,2],[5,4,1,3,2]


If my program is correct, the first few values of a(n), the number of minimally interpolable permutations of {1, 2, ..., n}:

a(1) = 1
a(2) = 2
a(3) = 4
a(4) = 18
a(5) = 48
a(6) = 216
a(7) = 584
a(8) = 2870

• Nice sequence number! | Although you specified fastest-code, you didn't specify which machine is it fastest on. What is exactly the winning criteria? – user202729 Mar 27 '18 at 0:36
• To add to user202729's comment, I suggest some tags that you can use to determine the winning criteria: fastest-code requires that submissions be tested on the same machine to compare the runtime (usually the OP of the challenge does this). fastest-algorithm would ask answerers to come up with code with the lowest time complexity as possible. code-golf would ask users to come up with code with the shortest source code (or equivalent) as possible. Other than that, this is indeed a nice challenge. – JungHwan Min Mar 27 '18 at 0:55
• Your example text uses zero-indexing though the image uses one-indexing. – Jonathan Frech Mar 27 '18 at 20:54
• Since all points are defined by permuations of the first natural numbers, is it not impossible for any two points to occupy the same height? – Jonathan Frech Mar 27 '18 at 20:57
• @JonathanFrech, indeed, it should be 1-indexed because these are permutations. And you're correct! Because we're dealing with permutations, the 0-degree polynomial condition comes for free. – Peter Kagey Mar 27 '18 at 22:04

## C#

using System;
using System.Diagnostics;
using BigInteger = System.Int32;

namespace Sandbox
{
class PPCG160382
{
public static void Main(params string[] args)
{
if (args.Length != 0)
{
foreach (var arg in args) Console.WriteLine(CountValidPerms(int.Parse(arg)));
}
else
{
int[] smallValues = new int[] { 1, 1, 2, 4, 18, 48 };
for (int n = 0; n < smallValues.Length; n++)
{
var observed = CountValidPerms(n);
var expected = smallValues[n];
Console.WriteLine(observed == expected ? $"{n}: Ok" :$"{n}: expected {expected}, observed {observed}, error {observed - expected}");
}
for (int n = smallValues.Length; n < 13; n++)
{
Stopwatch sw = new Stopwatch();
sw.Start();
Console.WriteLine(\$"{n}: {CountValidPerms(n)} in {sw.ElapsedMilliseconds}ms");
}
}
}

private static long CountValidPerms(int n)
{
// We work on the basis of exclusion by extrapolation.
var unused = (1 << n) - 1;
var excluded = new int[n];
int[] perm = new int[n];

// Symmetry exclusion: perm[0] < (n+1) / 2
if (n > 1) excluded[0] = (1 << n) - (1 << ((n + 1) / 2));

long count = 0;
CountValidPerms(ref count, perm, 0, unused, excluded);
return count;
}

private static void CountValidPerms(ref long count, int[] perm, int off, int unused, int[] excluded)
{
int n = perm.Length;
if (off == n)
{
count += CountSymmetries(perm);
return;
}

// Quick-aborts
var completelyExcluded = excluded[off];
for (int i = off + 1; i < n; i++)
{
if ((unused & ~excluded[i]) == 0) return;
completelyExcluded &= excluded[i];
}
if ((unused & completelyExcluded) != 0) return;

// Consider each unused non-excluded value as a candidate for perm[off]
var candidates = unused & ~excluded[off];
for (int val = 0; candidates > 0; val++, candidates >>= 1)
{
if ((candidates & 1) == 0) continue;

perm[off] = val;

var nextUnused = unused & ~(1 << val);

var nextExcluded = (int[])excluded.Clone();
// For each (non-trivial) subset of smaller indices, combine with off and extrapolate to off+1 ... excluded.Length-1
if (off < n - 1 && off > 0)
{
var points = new Point[off + 1];
var denoms = new BigInteger[off + 1];
points[0] = new Point { X = off, Y = perm[off] };
denoms[0] = 1;
ExtendExclusions(perm, off, 0, points, 1, denoms, nextExcluded);
}

// Symmetry exclusion: perm[0] < perm[-1] < n - 1 - perm[0]
if (off == 0 && n > 1)
{
nextExcluded[n - 1] |= (1 << n) - (2 << (n - 1 - val));
nextExcluded[n - 1] |= (2 << val) - 1;
}

CountValidPerms(ref count, perm, off + 1, nextUnused, nextExcluded);
}
}

private static void ExtendExclusions(int[] perm, int off, int idx, Point[] points, int numPoints, BigInteger[] denoms, int[] excluded)
{
if (idx == off) return;

// Subsets without
ExtendExclusions(perm, off, idx + 1, points, numPoints, denoms, excluded);

// Just add this to the subset
points[numPoints] = new Point { X = idx, Y = perm[idx] };
denoms = (BigInteger[])denoms.Clone();
// Update invariant: denoms[s] = prod_{t != s} points[s].X - points[t].X
denoms[numPoints] = 1;
for (int s = 0; s < numPoints; s++)
{
denoms[s] *= points[s].X - points[numPoints].X;
denoms[numPoints] *= points[numPoints].X - points[s].X;
}
numPoints++;

for (int target = off + 1; target < excluded.Length; target++)
{
BigInteger prod = 1;
for (int t = 0; t < numPoints; t++) prod *= target - points[t].X;

Rational sum = new Rational(0, 1);
for (int s = 0; s < numPoints; s++) sum += new Rational(prod / (target - points[s].X) * points[s].Y, denoms[s]);

if (sum.Denom == 1 && sum.Num >= 0 && sum.Num < excluded.Length) excluded[target] |= 1 << (int)sum.Num;
}

// Subsets with
ExtendExclusions(perm, off, idx + 1, points, numPoints, denoms, excluded);
}

private static int CountSymmetries(int[] perm)
{
if (perm.Length < 2) return 1;

int cmp = 0;
for (int i = 0, j = perm.Length - 1; i <= j; i++, j--)
{
cmp = perm.Length - 1 - perm[i] - perm[j];
if (cmp != 0) break;
}

return cmp > 0 ? 4 : cmp == 0 ? 2 : 0;
}

public struct Point
{
public int X;
public int Y;
}

public struct Rational
{
public Rational(BigInteger num, BigInteger denom)
{
if (denom == 0) throw new ArgumentOutOfRangeException(nameof(denom));

if (denom < 0) { num = -num; denom = -denom; }

var g = _Gcd(num, denom);
Num = num / g;
Denom = denom / g;
}

private static BigInteger _Gcd(BigInteger a, BigInteger b)
{
if (a < 0) a = -a;
if (b < 0) b = -b;
while (a != 0)
{
var tmp = b % a;
b = a;
a = tmp;
}
return b;
}

public BigInteger Num;
public BigInteger Denom;

public static Rational operator +(Rational a, Rational b) => new Rational(a.Num * b.Denom + a.Denom * b.Num, a.Denom * b.Denom);
}
}
}


Takes values of n as command-line arguments, or if run without arguments times itself up to n=10. Compiling as "Release" in VS 2017 and running on an Intel Core i7-6700 I calculate n=9 in 1.2 seconds, and n=10 in 13.6 seconds. n=11 is just over 2 minutes.

FWIW:

n    a(n)
9    10408
10   45244
11   160248
12   762554