Sample code and modification to the algorithm

Hi.
I have tweaked quite a bit to make the code closer to the definition on Wikipedia.
Thanks for consideration of merging it to master.