Skip to content

Commit ea89bba

Browse files
committed
Optimized AbundantNumber to N/2.
1 parent 1c2372e commit ea89bba

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

src/main/java/stringbasic/exercises/AbundantNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public AbundantNumber(int number) {
1010

1111
public int abundance() {
1212
int result = 0;
13-
for (int i = this.number-1; i>0; i--) {
13+
for (int i = 1; i <= this.number/2; i++) {
1414
if (this.number % i == 0)
1515
result += i;
1616
}

src/test/java/stringbasic/exercises/AbundantNumberTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ public AbundantNumberTest(int number, int abundance, boolean isAbundant) {
3939
public void testKnownCases() {
4040
AbundantNumber sut = new AbundantNumber(this.number);
4141
Assert.assertEquals(this.abundance, sut.abundance());
42+
Assert.assertEquals(this.isAbundant, sut.isAbundant());
4243
}
4344
}

0 commit comments

Comments
 (0)