Keep in mind you have to consider (n-1)
as upper limit. Let's see how the model program could be intepreted
if (n <= 2)
return 0;
This is trivial, since
1
is not divisible by
2
or
3
.
if (n == 3)
return 2;
This is trivial too, since in the
1,2
sequence, there is only
2
which is divisible.
int twoSum = (n - 1) % 2 == 0? (n - 1): 0;
This means
int twoSum;
if ( (n-1) % 2 == 0)
twoSum = (n-1);
else
twoSum = 0;
That is
"if (n-1) is divisible by 2
then keep it in order to add it to the sum".
The intepretation of
int threeSum = (n - 1) % 3 == 0? (n - 1): 0;
is similar.
return twoSum + threeSum + threePlusTwo(n - 1);
This makes the trick of adding two times a number which divisible both by
2
and
3
(please note such a requirement is not obvious from the question title).