Problem 5 in Python and C#