Some of my programs are required to have a strict time limit for completing given tasks.
If I am correct, converting certain loops to mathematical equations should reduce the time complexity for my program, yes? I can get the same result the loop finds, in just one operation?
I've checked many other solutions regarding this problem, sadly they all focus on solving the loop itself rather than the general steps that should be taken to convert loops into mathematical equations, I could not understand much.
I need to be able to convert loops myself, and I can't find anywhere on the internet to help with this one issue. References would be appreciated.
For example, this loop takes more than 7 seconds at some cases:
for (int j = 0; j < N * M; j++){
S += V;
V = (A * V + B) % (10007);
}
And this one as well takes more than one second:
for (int i = 1; i <= product; i++){
if (product % i == 0)
sum += i;
}
Please notice that my problem does not lie within these two loops, I need to know how to approach any convertible loop. Answers are also not limited to equations, any performance tips are appreciated.
EDIT: Here is an example of what I mean.