Primecoin: memory optimizations

Manually merged in sunny's changes, so slight changes there. Also
contains some optimizations from Mike270
(https://bitcointalk.org/index.php?topic=251850.msg2700874#msg2700874)