And a huge thank you to Volodya, Hermit, for both tasks!
P.S. I just sent the solution to the girl (yes, it was for her, why did I need Pascal?), she complained that it was too complicated and not for first-year students, but I was forgiven - and that's great :)
Thanks again to everyone.