primecoin optimizations from https://bitcointalk.org/index.php?topic=251850.msg2690393#msg2690393