- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
Latest commit
19 lines (18 loc) · 547 Bytes
/
Copy pathSolution.java
File metadata and controls
19 lines (18 loc) · 547 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classMyCalculatorimplementsAdvancedArithmetic {
// Time Complexity: O(n^(1/2))
// Space Complexity: O(1)
publicintdivisor_sum(intn) {
intsum = 0;
intsqrt = (int) Math.sqrt(n);
for (inti = 1; i <= sqrt; i++) {
if (n % i == 0) { // if "i" is a divisor
sum += i + n/i; // add both divisors
}
}
/* If sqrt is a divisor, we should only count it once */
if (sqrt * sqrt == n) {
sum -= sqrt;
}
returnsum;
}
}