# Proper writing of substitutions involving integers?

I would like to make a substitution like the following

(-3 + n)!/.(-x_Integer + n)!:>a


where I expect a to be the output. Unfortunately, Mathematica 10 just returns

(-3 + n)!

I even tried

(-3 + n)!/.Factorial[Plus[-x_Integer,n]]:>a


And the result is still

(-3 + n)!

What am I doing wrong? How to make this substitution work?

• What does MatchQ[-3, -x_Integer] tell you? – Marius Ladegård Meyer Oct 19 '16 at 15:11
• It works if you leave out the minus, i.e. (-3 + n)!/.Factorial[Plus[x_Integer,n]]:>a because Mathematica recognizes the negative integer as integer too. If you only want negative values to match, you could do x_?(IntegerQ[#]&&Negative[#]&) – AndreasP Oct 19 '16 at 15:11
• Thanks, good to know! Guess I'll just do /. (x_Integer + n)! /; x < 0:>a. – Kagaratsch Oct 19 '16 at 15:18

When having problems with a replacement rule, use FullForm to understand the patterns involved

(-3+n)!//FullForm

(*  Factorial[Plus[-3,n]]  *)

(-x_Integer+n)!//FullForm

(*  Factorial[Plus[n,Times[-1,Pattern[x,Blank[Integer]]]]]  *)


You can use

{(-3+n)!,(3+n)!}/.(_Integer+n)!:>a

(*  {a,a}  *)


Or, if the rule is only to apply for negative integers then use

{(-3+n)!,(3+n)!}/.(_Integer?Negative+n)!:>a

(*  {a,(3+n)!}  *)