Skip to content

Created PerfectNumberTest.java & Added function in PerfectNumber.java #3751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions src/main/java/com/thealgorithms/maths/PerfectNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@
* has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a
* perfect number.
*
* <p>
* link:https://en.wikipedia.org/wiki/Perfect_number
*/
public class PerfectNumber {

public static void main(String[] args) {
assert isPerfectNumber(6);
/* 1 + 2 + 3 == 6 */
assert !isPerfectNumber(8);
/* 1 + 2 + 4 != 8 */
assert isPerfectNumber(28);
/* 1 + 2 + 4 + 7 + 14 == 28 */
}

/**
* Check if {@code number} is perfect number or not
*
* @param number the number
* @return {@code true} if {@code number} is perfect number, otherwise false
*/
public static boolean isPerfectNumber(int number) {
if (number <= 0)
return false;
int sum = 0;
/* sum of its positive divisors */
for (int i = 1; i < number; ++i) {
Expand All @@ -36,4 +28,38 @@ public static boolean isPerfectNumber(int number) {
}
return sum == number;
}

/**
* Check if {@code n} is perfect number or not
*
* @param n the number
* @return {@code true} if {@code number} is perfect number, otherwise false
*/
public static boolean isPerfectNumber2(int n) {
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.
* Ex- Factors of 100 are 1, 2, 4, 5, 10, 20, 25, 50 and 100.
* Root of 100 is 10. So factors before 10 are 1, 2, 4 and 5.
* Now by dividing 100 by each factor before 10 we get:
* 100/1 = 100, 100/2 = 50, 100/4 = 25 and 100/5 = 20
* So we get 100, 50, 25 and 20 which are factors of 100 after 10
*/
for (int i = 2; i <= root; i++) {
if (n % i == 0) {
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;

return sum == n;
}
}
21 changes: 21 additions & 0 deletions src/test/java/com/thealgorithms/maths/PerfectNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class PerfectNumberTest {

@Test
public void perfectNumber() {
int trueTestCases[] = { 6, 28, 496, 8128, 33550336 };
int falseTestCases[] = { -6, 0, 1, 9, 123 };
for (Integer n : trueTestCases) {
assertTrue(PerfectNumber.isPerfectNumber(n));
assertTrue(PerfectNumber.isPerfectNumber2(n));
}
for (Integer n : falseTestCases) {
assertFalse(PerfectNumber.isPerfectNumber(n));
assertFalse(PerfectNumber.isPerfectNumber2(n));
}
}
}