Created
September 4, 2011 13:08
-
-
Save yenliangl/1192828 to your computer and use it in GitHub Desktop.
Perfect number finder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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