You are viewing a single comment's thread. Return to all comments →

I added some optimisations(there is the half of operations if the number n is odd).

What do you think about it?

class Calculator : public AdvancedArithmetic { public: int divisorSum(int n) { int sum = n + 1; const int s = sqrt(n); const int di = 1 + (n & 1); for(int i = di + 1; i <= s; i += di) { if (n % i == 0) { sum += i + n / i; } } if (s * s == n) sum -= s; return sum; } };

Nice optimization. I've updated my solution using your suggestion.

HackerRank solutions.

Thanks. I starred your project.

Also I think "n & 1" is faster than "n % 2".

(Of course, if a compiler doesn't do the optimisation).

PS. Fix the link. It's broken.

Thanks for star. Fixed the link. You're right, n & 1 is probably faster.

## Day 19: Interfaces

You are viewing a single comment's thread. Return to all comments →

I added some optimisations(there is the half of operations if the number n is odd).

What do you think about it?

Nice optimization. I've updated my solution using your suggestion.

HackerRank solutions.

Thanks. I starred your project.

Also I think "n & 1" is faster than "n % 2".

(Of course, if a compiler doesn't do the optimisation).

PS. Fix the link. It's broken.

Thanks for star. Fixed the link. You're right, n & 1 is probably faster.