File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
main/java/by/andd3dfx/numeric
test/java/by/andd3dfx/numeric Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ package by .andd3dfx .numeric ;
2+
3+ import by .andd3dfx .dynamic .ChangeWithMinNumberOfCoins ;
4+
5+ /**
6+ * See <a href="https://en.wikipedia.org/wiki/Coin_problem">article</a> in wiki
7+ * <p>
8+ * Find the largest monetary amount that cannot be obtained using only coins of specified denominations.
9+ */
10+ public class FrobeniusCoinProblem {
11+
12+ public static int find (int [] nominals ) {
13+ var capAmount = determineStartingValueEnhanced (nominals );
14+ for (var amount = capAmount ; amount >= 1 ; amount --) {
15+ if (ChangeWithMinNumberOfCoins .determine_usingMemoization (nominals , amount ) == -1 ) {
16+ return amount ;
17+ }
18+ }
19+ return 1 ;
20+ }
21+
22+ /**
23+ * Method wasn't deleted for demo purposes
24+ */
25+ private static int determineStartingValuePrimitive (int [] nominals ) {
26+ var result = 1 ;
27+ for (int nominal : nominals ) {
28+ result *= nominal ;
29+ }
30+ System .out .println (result );
31+ return result ;
32+ }
33+
34+ private static int determineStartingValueEnhanced (int [] nominals ) {
35+ return LeastCommonMultiple .find (nominals );
36+ }
37+ }
Original file line number Diff line number Diff line change 1+ package by .andd3dfx .numeric ;
2+
3+ import org .junit .Test ;
4+
5+ import static org .assertj .core .api .Assertions .assertThat ;
6+
7+ public class FrobeniusCoinProblemTest {
8+
9+ @ Test
10+ public void testFind () {
11+ // Formula g = a1*a2-a1-a2 works only for relatively prime numbers a1, a2
12+ assertThat (FrobeniusCoinProblem .find (new int []{2 , 5 })).isEqualTo (2 * 5 - 2 - 5 );
13+ assertThat (FrobeniusCoinProblem .find (new int []{3 , 7 })).isEqualTo (3 * 7 - 3 - 7 );
14+ assertThat (FrobeniusCoinProblem .find (new int []{6 , 7 })).isEqualTo (6 * 7 - 6 - 7 );
15+
16+ // McNuggets problem
17+ assertThat (FrobeniusCoinProblem .find (new int []{6 , 9 , 20 })).isEqualTo (43 );
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments