Skip to content
Closed
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: 46 additions & 0 deletions ProjectEuler/Problem19.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ProjectEuler;

import java.util.Arrays;

public class Problem19 {
public static void main(String[] args) {
assert solution() == 171;
}

public static int solution() {
int year = 1901;
boolean leapYear = false;
int totalSundays = 0;
int currentDay = 2; // start on a monday

while (year <= 2000) {
leapYear = false;
if (year % 4 == 0) {
if (year % 100 == 0 && year % 400 == 0) {
leapYear = true;
} else leapYear = year % 100 != 0 || year % 400 == 0;
}

for (int month = 1; month <= 12; month++) {
if (currentDay == 7) {
totalSundays++;
}
if (Arrays.asList(1, 3, 5, 7, 8, 10, 12).contains(month)) {
currentDay += 3;
} else if (Arrays.asList(4, 6, 9, 11).contains(month)) {
currentDay += 2;
} else if (leapYear) {
currentDay += 1;
}

if (currentDay > 7) {
currentDay -= 7;
}
}

year++;
}

return totalSundays;
}
}
36 changes: 36 additions & 0 deletions ProjectEuler/Problem31.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ProjectEuler;

/**
* In the United Kingdom the currency is made up of pound (£) and pence (p). There are eight coins
* in general circulation:
*
* <p>1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
*
* <p>It is possible to make £2 in the following way:
*
* <p>1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
*
* <p>How many different ways can £2 be made using any number of coins?
*
* <p>link: https://projecteuler.net/problem=31
*/
public class Problem31 {
public static void main(String[] args) {
assert solution() == 73682;
}

public static int solution() {
int target = 200;
int[] coins = {1, 2, 5, 10, 20, 50, 100, 200};
int[] combos = new int[201];
combos[0] = 1;

for (int coin : coins) {
for (int i = coin; i <= target; i++) {
combos[i] += combos[i - coin];
}
}

return combos[200];
}
}