mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
@@ -17,8 +17,7 @@ public class PerfectNumber {
|
||||
* @return {@code true} if {@code number} is perfect number, otherwise false
|
||||
*/
|
||||
public static boolean isPerfectNumber(int number) {
|
||||
if (number <= 0)
|
||||
return false;
|
||||
if (number <= 0) return false;
|
||||
int sum = 0;
|
||||
/* sum of its positive divisors */
|
||||
for (int i = 1; i < number; ++i) {
|
||||
@@ -28,7 +27,7 @@ public class PerfectNumber {
|
||||
}
|
||||
return sum == number;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if {@code n} is perfect number or not
|
||||
*
|
||||
@@ -36,11 +35,10 @@ public class PerfectNumber {
|
||||
* @return {@code true} if {@code number} is perfect number, otherwise false
|
||||
*/
|
||||
public static boolean isPerfectNumber2(int n) {
|
||||
if (n <= 0)
|
||||
return false;
|
||||
if (n <= 0) return false;
|
||||
int sum = 1;
|
||||
double root = Math.sqrt(n);
|
||||
|
||||
|
||||
/*
|
||||
* We can get the factors after the root by dividing number by its factors
|
||||
* before the root.
|
||||
@@ -55,10 +53,10 @@ public class PerfectNumber {
|
||||
sum += i + n / i;
|
||||
}
|
||||
}
|
||||
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root from sum
|
||||
if (root == (int) root)
|
||||
sum -= root;
|
||||
|
||||
// if n is a perfect square then its root was added twice in above loop, so subtracting root
|
||||
// from sum
|
||||
if (root == (int) root) sum -= root;
|
||||
|
||||
return sum == n;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user