#### 題目描述

divisorSum方法只能輸入一個整數，找出這個整數的所有因數，並將它們加總之後把加總結果回傳。舉例來說，6的因數有1, 2, 3, 6，所以divisorSum方法計算後會回傳12。傳入的整數n的最大值為1000。

6

12

#### 參考答案

```import java.util.Scanner;

public abstract int divisorSum(int n);
}

@Override
public int divisorSum(final int n) {
if (n == 1) {
return 1;
} else if (n < 1) {
return 0;
}
final int sqrtN = (int) Math.floor(Math.sqrt(n));
// 先找最小質因數
int sum = 0;
for (int i = 2; i <= sqrtN; ++i) {
if (n % i == 0) {
sum = i;
break;
}
}
if (sum >= 2) { // 若不是質數，有最小質因數
// 計算從最小質因數+1到原數除最小質因數的因數
final int nn = n / sum;
for (int i = sum + 1; i <= nn; ++i) {
if (n % i == 0) {
sum += i;
}
}
}
sum += n + 1; //把1和自己本身算進去
return sum;
}

}

public class Solution {

public static void main(final String[] args) {
final MyCalculator my_calculator = new MyCalculator();
System.out.print("I implemented: ");
ImplementedInterfaceNames(my_calculator);
final Scanner sc = new Scanner(System.in);
final int n = sc.nextInt();
System.out.print(my_calculator.divisorSum(n) + "\n");

}

static void ImplementedInterfaceNames(final Object o) {

final Class[] theInterfaces = o.getClass().getInterfaces();
for (int i = 0; i < theInterfaces.length; ++i) {
final String interfaceName = theInterfaces[i].getName();
System.out.println(interfaceName);
}
}
}```