%I #51 Aug 05 2022 07:45:34
%S 0,0,-1,0,3,0,17,-10,134,354,1329,4155,3924,19797,-94380,787794,
%T 2901480,-1907466,38192984,204434670,-304139881,115819260,
%U -12372023755,6328965122,-397725674235,1196412908415,6734756394444,-6589458328753,48604536424455,-1553224821563460,2464230045322035
%N Let t be the closest triangular number to n! (in case n=2, the only case where we have a tie, take the larger t); then a(n) = n! - t.
%C It is conjectured that 0! = 1, 1! = 1, 3! = 6 and 5! = 120 are the only numbers that are both factorial (A000142) and triangular (A000217) numbers.
%F a(n) = n! - m*(m+1)/2 where m = floor(sqrt(2 * n!)).
%F a(n) = A000142(n) - A000217(A129960(n)).
%e a(7) = 7! - 100*101 / 2 = 5040 - 5050 = -10.
%t ctn[n_]:=Module[{c=Floor[(Sqrt[1+8n!]-1)/2],tr1,tr2,trp},tr1=(c(c+1))/2;tr2=((c+1)(c+2))/2;trp=Nearest[{tr1,tr2},n!];n!-trp]; Join[{0,0,-1},Flatten[Array[ctn,30,3]]] (* _Harvey P. Dale_, Aug 22 2021 *)
%o (PARI) a(n) = my(m = sqrtint(2*n!)); n! - m*(m+1)/2; \\ _Michel Marcus_, Nov 09 2020
%o (Python)
%o from math import factorial, isqrt
%o def A338489(n): return (f:=factorial(n))-((m:=isqrt(f<<1))*(m+1)>>1) # _Chai Wah Wu_, Aug 04 2022
%Y Cf. A000142, A000217, A129960.
%K sign,easy
%O 0,5
%A _Ruediger Jehn_, Nov 09 2020