Goldman Sachs interview question

Asked two difficult maths questions and I need to write the program right away.