wrote sum algorithm