Skip to content

Commit c6e85e3

Browse files
authored
Merge pull request #1 from stringbasic/abundant-number
Implemented Abundant number
2 parents f9bc2a4 + 5b494f5 commit c6e85e3

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package stringbasic.exercises;
2+
3+
public class AbundantNumber {
4+
5+
private int number;
6+
7+
public AbundantNumber(int number) {
8+
this.number = number;
9+
}
10+
11+
public int abundance() {
12+
int result = 0;
13+
for (int i = 1; i <= Math.sqrt(this.number); i++) {
14+
if (this.number % i == 0) {
15+
result += i;
16+
if (this.number / i != i)
17+
result += this.number / i;
18+
}
19+
}
20+
21+
return result - this.number;
22+
}
23+
24+
public boolean isAbundant() {
25+
return this.abundance() > this.number;
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package stringbasic.exercises;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.Parameterized;
8+
import org.junit.runners.Parameterized.Parameters;
9+
10+
import java.util.Collection;
11+
12+
@RunWith(Parameterized.class)
13+
public class AbundantNumberTest {
14+
15+
private int number;
16+
private int abundance;
17+
private boolean isAbundant;
18+
19+
@Parameters
20+
public static Collection<Object[]> abundantNumbers () {
21+
return ImmutableList.of(
22+
new Object[] {10, 8, false},
23+
new Object[] {12, 16, true},
24+
new Object[] {18, 21, true},
25+
new Object[] {20, 22, true},
26+
new Object[] {21, 11, false},
27+
new Object[] {24, 36, true},
28+
new Object[] {30, 42, true}
29+
);
30+
}
31+
32+
public AbundantNumberTest(int number, int abundance, boolean isAbundant) {
33+
this.number = number;
34+
this.abundance = abundance;
35+
this.isAbundant = isAbundant;
36+
}
37+
38+
@Test
39+
public void testKnownCases() {
40+
AbundantNumber sut = new AbundantNumber(this.number);
41+
Assert.assertEquals("number: " + this.number, this.abundance, sut.abundance());
42+
Assert.assertEquals("number: " + this.number, this.isAbundant, sut.isAbundant());
43+
}
44+
}

0 commit comments

Comments
 (0)