P500: Added mathematical explanation, simplified Java solution significantly and Python solution slightly.