-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from jaebradley/between-two-sets
Between two sets
- Loading branch information
Showing
12 changed files
with
258 additions
and
6 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/algorithms/implementations/BetweenTwoSetsImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package algorithms.implementations; | ||
|
||
import algorithms.interfaces.BetweenTwoSets; | ||
import algorithms.interfaces.GreatestCommonDivisorCalculator; | ||
import algorithms.interfaces.LeastCommonMultipleCalculator; | ||
|
||
import java.util.Set; | ||
|
||
public class BetweenTwoSetsImpl implements BetweenTwoSets { | ||
private final GreatestCommonDivisorCalculator gcdCalculator; | ||
private final LeastCommonMultipleCalculator lcmCalculator; | ||
|
||
public BetweenTwoSetsImpl(final GreatestCommonDivisorCalculator gcdCalculator, final LeastCommonMultipleCalculator lcmCalculator) { | ||
this.gcdCalculator = gcdCalculator; | ||
this.lcmCalculator = lcmCalculator; | ||
} | ||
|
||
@Override | ||
public int betweenValuesCount(final Set<Long> firstValues, final Set<Long> secondValues) { | ||
if (firstValues.size() < 2 || secondValues.size() < 2) { | ||
return 0; | ||
} | ||
|
||
final long gcd = this.gcdCalculator.calculateGreatestCommonDivisor(secondValues); | ||
final long lcm = this.lcmCalculator.calculateLeastCommonMultiple(firstValues); | ||
|
||
int count = 0; | ||
long lcmMultiple = lcm; | ||
while (lcmMultiple <= gcd) { | ||
if (gcd % lcmMultiple == 0) { | ||
count++; | ||
} | ||
lcmMultiple += lcm; | ||
} | ||
|
||
return count; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package algorithms.interfaces; | ||
|
||
import java.util.Set; | ||
|
||
public interface BetweenTwoSets { | ||
int betweenValuesCount(final Set<Long> firstValues, final Set<Long> secondValues); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/algorithms/interfaces/GreatestCommonDivisorCalculator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package algorithms.interfaces; | ||
|
||
import java.util.Collection; | ||
|
||
public interface GreatestCommonDivisorCalculator { | ||
long calculateGreatestCommonDivisor(final long a, final long b); | ||
long calculateGreatestCommonDivisor(final Collection<Long> values); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/algorithms/interfaces/LeastCommonMultipleCalculator.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package algorithms.interfaces; | ||
|
||
import java.util.Collection; | ||
|
||
public interface LeastCommonMultipleCalculator { | ||
long calculateLeastCommonMultiple(final long a, final long b); | ||
long calculateLeastCommonMultiple(final Collection<Long> values); | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/algorithms/utilities/GreatestCommonDivisorCalculatorImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package algorithms.utilities; | ||
|
||
import algorithms.interfaces.GreatestCommonDivisorCalculator; | ||
|
||
import java.util.Collection; | ||
|
||
public class GreatestCommonDivisorCalculatorImpl implements GreatestCommonDivisorCalculator { | ||
@Override | ||
public long calculateGreatestCommonDivisor(final long a, final long b) { | ||
if (a == 0 && b != 0) { | ||
return b; | ||
} | ||
|
||
if (b == 0 && a != 0) { | ||
return a; | ||
} | ||
|
||
long first = a; | ||
long second = b; | ||
|
||
while (first != second) { | ||
if (first > second) { | ||
first = first - second; | ||
} else { | ||
second = second - first; | ||
} | ||
} | ||
|
||
return first; | ||
} | ||
|
||
@Override | ||
public long calculateGreatestCommonDivisor(final Collection<Long> values) { | ||
if (values.size() < 2) { | ||
throw new RuntimeException("No values"); | ||
} | ||
|
||
return values.stream() | ||
.reduce(this::calculateGreatestCommonDivisor) | ||
.get(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/algorithms/utilities/LeastCommonMultipleCalculatorImpl.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package algorithms.utilities; | ||
|
||
import algorithms.interfaces.GreatestCommonDivisorCalculator; | ||
import algorithms.interfaces.LeastCommonMultipleCalculator; | ||
|
||
import java.util.Collection; | ||
|
||
public class LeastCommonMultipleCalculatorImpl implements LeastCommonMultipleCalculator { | ||
private final GreatestCommonDivisorCalculator greatestCommonDivisorCalculator; | ||
|
||
public LeastCommonMultipleCalculatorImpl(final GreatestCommonDivisorCalculator greatestCommonDivisorCalculator) { | ||
this.greatestCommonDivisorCalculator = greatestCommonDivisorCalculator; | ||
} | ||
|
||
@Override | ||
public long calculateLeastCommonMultiple(final long a, final long b) { | ||
return ( a * b ) / this.greatestCommonDivisorCalculator.calculateGreatestCommonDivisor(a, b); | ||
} | ||
|
||
@Override | ||
public long calculateLeastCommonMultiple(final Collection<Long> values) { | ||
if (values.size() < 2) { | ||
throw new RuntimeException("No values"); | ||
} | ||
|
||
return values.stream() | ||
.reduce(this::calculateLeastCommonMultiple) | ||
.get(); | ||
} | ||
} |
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
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
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
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
63 changes: 63 additions & 0 deletions
63
src/test/java/algorithms/utilities/GreatestCommonDivisorCalculatorImplTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package algorithms.utilities; | ||
|
||
import algorithms.interfaces.GreatestCommonDivisorCalculator; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class GreatestCommonDivisorCalculatorImplTest { | ||
final GreatestCommonDivisorCalculator calculator = new GreatestCommonDivisorCalculatorImpl(); | ||
|
||
@Test | ||
public void itShouldReturnOneForRelativelyPrimeNumbers() { | ||
final long gcd = calculator.calculateGreatestCommonDivisor(2, 3); | ||
assertEquals(gcd, 1); | ||
} | ||
|
||
@Test | ||
public void itShouldReturnNumberForTwoIdenticalValues() { | ||
final long input = 3; | ||
final long gcd = calculator.calculateGreatestCommonDivisor(input, input); | ||
assertEquals(gcd, input); | ||
} | ||
|
||
@Test | ||
public void itShouldReturnGCD() { | ||
final long gcd = calculator.calculateGreatestCommonDivisor(1071, 462); | ||
assertEquals(gcd, 21); | ||
} | ||
|
||
@Test | ||
public void itShouldReturnNonZeroValueWhenOneInputIsZero() { | ||
final long input = 1; | ||
long gcd = calculator.calculateGreatestCommonDivisor(0, input); | ||
assertEquals(gcd, input); | ||
|
||
gcd = calculator.calculateGreatestCommonDivisor(input, 0); | ||
assertEquals(gcd, input); | ||
} | ||
|
||
@Test | ||
public void itShouldThrowWhenThereAreNotTwoValues() { | ||
try { | ||
calculator.calculateGreatestCommonDivisor(new ArrayList<>()); | ||
} catch (RuntimeException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void itShouldReturnGCDForMultipleValues() { | ||
final long a = 3; | ||
final long b = 6; | ||
final long c = 9; | ||
final List<Long> values = new ArrayList<>(); | ||
values.add(a); | ||
values.add(b); | ||
values.add(c); | ||
assertEquals(calculator.calculateGreatestCommonDivisor(values), a); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/algorithms/utilities/LeastCommonMultipleCalculatorImplTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package algorithms.utilities; | ||
|
||
import algorithms.interfaces.GreatestCommonDivisorCalculator; | ||
import algorithms.interfaces.LeastCommonMultipleCalculator; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class LeastCommonMultipleCalculatorImplTest { | ||
final long gcdConstant = 1; | ||
final GreatestCommonDivisorCalculator constantGcdCalculator = new GreatestCommonDivisorCalculator() { | ||
@Override | ||
public long calculateGreatestCommonDivisor(final long a, final long b) { | ||
return gcdConstant; | ||
} | ||
|
||
@Override | ||
public long calculateGreatestCommonDivisor(final Collection<Long> values) { | ||
return 0; | ||
} | ||
}; | ||
|
||
final LeastCommonMultipleCalculator calculator = new LeastCommonMultipleCalculatorImpl(constantGcdCalculator); | ||
|
||
@Test | ||
public void itShouldCalculateLCM() { | ||
final long a = 3; | ||
final long b = 3; | ||
final long expected = ( a * b ) / gcdConstant; | ||
assertEquals(calculator.calculateLeastCommonMultiple(a, b), expected); | ||
} | ||
|
||
@Test | ||
public void itShouldThrowWhenValuesAreEmpty() { | ||
try { | ||
calculator.calculateLeastCommonMultiple(new ArrayList<>()); | ||
} catch (RuntimeException e) { | ||
// expected | ||
} | ||
} | ||
|
||
@Test | ||
public void itShouldReturnLCMValues() { | ||
final long a = 3; | ||
final long b = 6; | ||
final long c = 9; | ||
final List<Long> values = new ArrayList<>(); | ||
values.add(a); | ||
values.add(b); | ||
values.add(c); | ||
final long expected = a * b * c; | ||
assertEquals(calculator.calculateLeastCommonMultiple(values), expected); | ||
} | ||
} |