The M 4 Core Project with HST - III. Search for variable stars in the primary field