made faster

Added %cython and made the parameter long. Also raised ValueError instead of Exception, as was suggested.