EDIT: Updated code, which ran in a more respectable 0.22 seconds.
Taking out reader.nextLine(); improved the runtime by around 0.2 seconds.
Adding a special treatment to perfect squares seemed to have no noticable impact on speed (might depend on test cases?).
It turned out using BufferedReader and BufferedWriter had an immense impact on IO speed, reducing runtime to 0.22 seconds.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main
{
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int testcases = Integer.parseInt(in.readLine());
int[] values = new int[testcases];
for(int i = 0; i<testcases; i++){
values[i]=Integer.parseInt(in.readLine());
}
for(int i = 0; i<testcases; i++){
int sum = 1;
int testcase = values[i];
double lim = Math.sqrt(testcase);
for(int k = 2; k<lim; k++){
if(testcase % k == 0){
sum += k + testcase / k;
}
}
int intLim = (int) lim;
if (intLim * intLim == testcase) {
sum += lim;
}
if(testcase == 1)
sum=0;
out.write(sum + "\n");
}
out.flush();
}
}