diff --git a/ProjectEuler/Problem19.java b/ProjectEuler/Problem19.java new file mode 100644 index 000000000000..0dab323a9029 --- /dev/null +++ b/ProjectEuler/Problem19.java @@ -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; + } +} diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java new file mode 100644 index 000000000000..8efef70222b3 --- /dev/null +++ b/ProjectEuler/Problem31.java @@ -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: + * + *

1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p). + * + *

It is possible to make £2 in the following way: + * + *

1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p + * + *

How many different ways can £2 be made using any number of coins? + * + *

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]; + } +}