First of all, there are a number of issues with your question. You could have avoided most of them by just reading and following the advice offered when entering your question here! By not doing so you're making it harder for anyone willing to help, and, as a result, the likelyhood of someone responding with a useful answer is considerably less.
Besides, making it easier for the people who try to help you is a matter of respect. If you just dump your code here, that is disrepectful toward those who are supposed to help you! Please put in some more effort next time you ask a question.
Second, when you post a question, do add the actual question to your code! We can't read your mind, nor do we see your screen, or the original task description that you are working on! Describe the result you get, and the result you expect, as well as any error messages you are seeing. Any part of that info that you omit will just leave us in the dark and will likely result in suggestions that don't help you.
Third, when you write code, do yourself and everyone else a favor and use names that are self-explanatory! "getResult" or "a" aren't useful at all! Use names that offer an insight about what they are used for, e. g. "sumOfDivisors". In case of "a", "sum_of_divisors" would also be a fitting name, but since that is exactly what the function does, you could simply name it "result", implying that this is the variable to store the sum of divisors. You did use "sum" and "final_sum" in your main function, but in this case the prefix "final" doesn't help in clarifying the difference between the two, as you are summing totally different numbers! "sum" should be "sum_of_divisors" and "final_sum" should be "sum_of_good_numbers" - that would instantly clarify what you are storing and calculating here!
Last, for performance, try these things:
1. Learn to use a profiler. You can of course try and make an educated guess which parts of your code take the most time, but even very experienced programmers tend to experience surprises when an actual profiler tells them where to look. The main reason for that is that modern compilers are typically much better at recognizing and optimizing inefficient code segments than the programmers themselves, and the performance bottlenecks that remain after compiler optimizations are much harder to spot.
2. The first thing to look at when you have performance issues, is redundand code, and that means, first, and foremost, code within loops - since that is code that is executed repeatedly. Check whether the loop code contains any statements that could be moved outside the loop. Calculating them once, before the start of the loop saves you needless repetitions of the same calculations. Note that any calculations performed within the for statement (or the conditions in do and while loops) are part of that loop code!
Example:
- your comparison i<=sqrt(n) is performed for every iteration. You could move the calculation invoked in that comparison out of the loop.
- you could calculate n/i just once and store it in a helper variable, or adjust your code that you don't need to recaluclate it multiple times.
3. check whether your loop code contains any nontrivial calculations or function calls. Consider replacing these with something more efficient.
Example, when you make a comparison involving stuff like square roots or divisions, try to reformulate them in such a manner that they use multiplications instead. E.g. instead of i<=sqrt(n), you could write i*i<=n. Of course, in this case, if you move the sqrt calculation outside the loop, it would be just one sqrt, but if you reform the comparison using i*i, then you have many multiplications, which may in this case not be an improvement! So be careful where to use this idea.
4. Function calls are considerably more time consuming than simple operations like * or /. When you have a function call inside a loop, check whether declaring it inline helps.
Example: your main function calls getResult() in a loop. Try declaring getResult() as inline.
5. When you have a loop, think about ways to reuse results from earlier iterations to help calculate results for later iterations.
Example: for every even number n=2*k, the divisors of k are also divisors of n. Warning: it's not trivial to find all additional divisors, and it requires storing the divisors of previous numbers - that could require a large amount of memory and just to maintan that memory might cost more performance than you could gain. However, if you think along the original idea, you could come up with an entirely different algorithm:
Instead of finding all divisors of all numbers up to n, construct all 'good' numbers n by combining divisors! For this idea it is useful to have heard of "
Gödel numbering[
^]" Basically this is an encoding that represents a given number n by its prime factors. E. g. the number 12 has the Gödel code {"2","1"} because 12=2^2*3^1, and 15 has the Gödel code {"0","1","1"} because 15=2^0*3^1*5^1.
The nice thing about Gödel representations is that calculating the sum of divisors is a lot easier. The bad thing is that, while you can enumerate (or order) numbers using their Gödel codes, that ordering does not correspond to the natural order of the actual numbers. Therefore it's a bit tricky to enumerate "all numbers up to N"
I'm not going to outline the (rather complex) steps for implementing this solution, as I don't think the author of this task was actually aiming for such a solution. Instead I'm going to suggest.
6. Use parallelization to improve your performance. Like 5, this might not be something the author of the task was aiming for. But if he did, simply use multiple threads to call getResult() for different numbers in your main loop.