If ever you decide to post code again, please have the courtesy to comment it.
A comment looks like this:
I glanced at the code but don't have the patience, after dinner with wine, to try to figure out what it's trying to do, which sadly is a prerequisite to being able to help. Sure, I could spend many minutes studying it, but frankly I couldn't be bothered when it's not even commented.
Edit1: Thanks for describing what your code is trying to do. Here are some suggestions:
1. How could you eliminate the
if(i == 1)
check?
2. If you removed
a
entirely, how could you write
sum = sum + a
?
3. If 1 is allowed as a divisor, how can you streamline the initialization of
sum1
and the
for(int j = 1…)
loop?
4. In
for(int j = 1, j < i…)
, how could you reduce the maximum value of
j
?
Edit2: If you knew the result of
i/j
, what would be the stopping point of the
for(int j…)
loop?
There are probably other optimizations that rely on analysis, such as numbers that can be immediately excluded if not divisible by both 2 and 3, but this starts to drag in as much math as software, which may not be the intention.