factored pseudo-random formula into its own function
implemented new version of pow with O(lg n) running time