optimize code in calculate_coins.py