From 1526892aba71df044a1e50ba2f9aae06d56c7071 Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 15 Mar 2021 17:14:55 +0000 Subject: [PATCH 1/6] Solution to Project Euler problem 31 --- ProjectEuler/Problem31.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ProjectEuler/Problem31.java diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java new file mode 100644 index 000000000000..6f58ddbebe5d --- /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 solution1() == 1; + } + + public static int solution1() { + 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 j = coin; j <= target; j++) { + combos[j] += combos[j - coin]; + } + } + + return combos[200]; + } +} From c82a282f74714777924f966c3707b786e172ccf8 Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 15 Mar 2021 20:12:23 +0000 Subject: [PATCH 2/6] Changed assert to correct value --- ProjectEuler/Problem31.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java index 6f58ddbebe5d..22fc709d9944 100644 --- a/ProjectEuler/Problem31.java +++ b/ProjectEuler/Problem31.java @@ -16,7 +16,7 @@ */ public class Problem31 { public static void main(String[] args) { - assert solution1() == 1; + assert solution1() == 73682; } public static int solution1() { From ca5fa17d2e4a132a25435cb9eff5a4df271ea092 Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 15 Mar 2021 20:39:43 +0000 Subject: [PATCH 3/6] Recommitting to see if checks run properly --- ProjectEuler/Problem31.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java index 22fc709d9944..ed8bf3e071fa 100644 --- a/ProjectEuler/Problem31.java +++ b/ProjectEuler/Problem31.java @@ -12,7 +12,7 @@ * *
How many different ways can £2 be made using any number of coins? * - *
link: https://projecteuler.net/problem=31 + *
link: https://projecteuler.net/problem=31 */ public class Problem31 { public static void main(String[] args) { From aef3fe446365a9a12c9d801ae06a7efd27d4570e Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 15 Mar 2021 21:29:27 +0000 Subject: [PATCH 4/6] Fixed formatting --- ProjectEuler/Problem31.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java index ed8bf3e071fa..22fc709d9944 100644 --- a/ProjectEuler/Problem31.java +++ b/ProjectEuler/Problem31.java @@ -12,7 +12,7 @@ * *
How many different ways can £2 be made using any number of coins? * - *
link: https://projecteuler.net/problem=31 + *
link: https://projecteuler.net/problem=31 */ public class Problem31 { public static void main(String[] args) { From e2a0adbef44f225bf96f54502057c4bd11284136 Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 15 Mar 2021 23:29:01 +0000 Subject: [PATCH 5/6] All checks didn't run last time --- ProjectEuler/Problem31.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ProjectEuler/Problem31.java b/ProjectEuler/Problem31.java index 22fc709d9944..8efef70222b3 100644 --- a/ProjectEuler/Problem31.java +++ b/ProjectEuler/Problem31.java @@ -16,18 +16,18 @@ */ public class Problem31 { public static void main(String[] args) { - assert solution1() == 73682; + assert solution() == 73682; } - public static int solution1() { + 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 j = coin; j <= target; j++) { - combos[j] += combos[j - coin]; + for (int i = coin; i <= target; i++) { + combos[i] += combos[i - coin]; } } From c2bfb4d114773f1a96117d00751349d6d7c12f2c Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 29 Mar 2021 15:22:38 +0100 Subject: [PATCH 6/6] Added Problem 19 --- ProjectEuler/Problem19.java | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ProjectEuler/Problem19.java 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; + } +}