Counting from 1 to n without any consecutive numbers
Mathematica, 58 bytes, polynomial(n) time
Abs[Sum[(k-1)Hypergeometric2F1[k,k-#,2,2](#-k)!,{k,#}]-1]&
How it works
Rather than iterating over permutations with brute force, we use the inclusion–exclusion principle to count them combinatorially.
Let S be the set of all permutations of [1, …, n] with σ1 = 1, σn = n, and let Si be the set of permutations σ ∈ S such that |σi − σi + 1| = 1. Then the count we are looking for is
|S| − |S1 ∪ ⋯ ∪ Sn − 1| = ∑2 ≤ k ≤ n + 1; 1 ≤ i2 < ⋯ < ik − 1 < n (−1)k − 2|Si2 ∩ ⋯ ∩ Sik − 1|.
Now, |Si2 ∩ ⋯ ∩ Sik − 1| only depends on k and on the number j of runs of consecutive indices in [i1, i2, …, ik − 1, ik] where for convenience we fix i1 = 0 and ik = n. Specifically,
|Si2 ∩ ⋯ ∩ Sik − 1| = 2j − 2(n − k)!, for 2 ≤ j ≤ k ≤ n,
|Si2 ∩ ⋯ ∩ Sik − 1| = 1, for j = 1, k = n + 1.
The number of such index sets [i1, i2, …, ik − 1, ik] with j runs is
(k − 1Cj − 1)(n − kCj − 2), for 2 ≤ j ≤ k ≤ n,
1, for j = 1, k = n + 1.
The result is then
(−1)n − 1 + ∑2 ≤ k ≤ n ∑2 ≤ j ≤ k (−1)k − 2(k − 1Cj − 1)(n − kCj − 2)2j − 2(n − k)!
The inner sum over j can be written using the hypergeometric 2F1 function:
(−1)n − 1 + ∑2 ≤ k ≤ n (−1)k(k − 1)2F1(2 − k, k − n; 2; 2)(n − k)!
to which we apply a Pfaff transformation that lets us golf away the powers of −1 using an absolute value:
(−1)n − 1 + ∑2 ≤ k ≤ n (−1)n(k − 1)2F1(k, k − n; 2; 2)(n − k)!
= |−1 + ∑1 ≤ k ≤ n (k − 1)2F1(k, k − n; 2; 2)(n − k)!|.
Demo
In[1]:= Table[Abs[Sum[(k-1)Hypergeometric2F1[k,k-#,2,2](#-k)!,{k,#}]-1]&[n],{n,50}]
Out[1]= {1, 0, 0, 0, 0, 2, 10, 68, 500, 4174, 38774, 397584, 4462848,
> 54455754, 717909202, 10171232060, 154142811052, 2488421201446,
> 42636471916622, 772807552752712, 14774586965277816, 297138592463202402,
> 6271277634164008170, 138596853553771517492, 3200958202120445923684,
> 77114612783976599209598, 1934583996316791634828454,
> 50460687385591722097602304, 1366482059862153751146376304,
> 38366771565392871446940748410, 1115482364570332601576605376898,
> 33544252621178275692411892779180, 1042188051349139920383738392594332,
> 33419576037745472521641814354312790,
> 1105004411146009553865786545464526206,
> 37639281863619947475378460886135133496,
> 1319658179153254337635342434408766065896,
> 47585390139805782930448514259179162696722,
> 1763380871412273296449902785237054760438426,
> 67106516021125545469475040472412706780911268,
> 2620784212531087457316728120883870079549134420,
> 104969402113244439880057492782663678669089779118,
> 4309132147486627708154774750891684285077633835734,
> 181199144276064794296827392186304334716629346180848,
> 7800407552443042507640613928796820288452902805286368,
> 343589595090843265591418718266306051705639884996218154,
> 15477521503994968035062094274002250590013877419466108978,
> 712669883315580566495978374316773450341097231239406211100,
> 33527174671849317156037438120623503416356879769273672584588,
> 1610762789255012501855846297689494046193178343355755998487686}
Jelly, 17 16 bytes
ṖḊŒ!ð1;;⁹IỊṀðÐḟL
A monadic link.
Try it online!
How?
ṖḊŒ!ð1;;⁹IỊṀðÐḟL - Link: number n
Ṗ - pop (implicit range build) -> [1,n-1]
Ḋ - dequeue -> [2,n-1]
Œ! - all permutations of [2,n-1]
ð ðÐḟ - filter discard those entries for which this is truthy:
1; - 1 concatenated with the entry
;⁹ - ...concatenated with right (n)
I - incremental differences
Ị - is insignificant (absolute value <=1)
Ṁ - maximum
L - length (the number of valid arrangements)
MATL, 16 bytes
qtq:Y@0&Yc!d|qAs
Try it online!
For inputs exceeding 12
it runs out of memory.
Explanation
q % Implicitly input n. Push n-1
tq % Duplicate and subtract 1: pushes n-2
: % Range [1 2 ... n-2]
Y@ % Matrix with all permutations, each in a row
0 % Push 0
&Yc % Append n-1 and predend 0 to each row
! % Tranpose
d % Consecutive differences along each column
| % Absolute value
q % Subtract 1
A % All: true if all values in each column are non-zero
s % Sum. Implicitly display