Square and Multiply algorithm