Skip to content

Instantly share code, notes, and snippets.

@yenliangl
Created September 4, 2011 13:08
Show Gist options
  • Save yenliangl/1192828 to your computer and use it in GitHub Desktop.
Save yenliangl/1192828 to your computer and use it in GitHub Desktop.
Perfect number finder
public class PerfectNumberFinder {
public static boolean isPerect(int number) {
// get factors
List<Integer> factors = new ArrayList<Integer>();
factors.add(1);
factors.add(number);
for (int i = 2; i < Matj.sqrt(number); i++) {
if (number % i == 0) {
factors.add(i);
if (number / i != i) {
factors.add(number / i);
}
}
}
// sum the factors
int sum = 0;
for (Integer i : factors) {
sum += i;
}
// if it's perfect number?
return sum - number == number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment