Optimising the code

Problem Link: IT solves Maths

Here is my solution:code for problem

I have used two for loops. It seems this code can be more optimised.
Please share a better approach to optimise this problem code.