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/8] 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/8] 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/8] 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/8] 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/8] 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 f4b9f1d8394c49be75473f6d1350b86f271f816a 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:53:44 +0100 Subject: [PATCH 6/8] 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; + } +} From 97ddb732e3fc5daeaf364d2277d985213e09249d Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 12 Apr 2021 10:55:15 +0100 Subject: [PATCH 7/8] Added Problem 26 --- ProjectEuler/Problem26.java | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 ProjectEuler/Problem26.java diff --git a/ProjectEuler/Problem26.java b/ProjectEuler/Problem26.java new file mode 100644 index 000000000000..3e62bc8376f6 --- /dev/null +++ b/ProjectEuler/Problem26.java @@ -0,0 +1,36 @@ +package ProjectEuler; + +public class Problem26 { + + public static void main(String[] args) { + assert solution() == 983; + } + + public static int solution() { + int sequenceSize = 0; + int d = 0; + + for (int i = 1000; i > 1; i--) { + if (sequenceSize >= i) { + break; + } + + int[] remaindersArray = new int[i]; + int val = 1; + int pos = 0; + + while (remaindersArray[val] == 0 && val != 0) { + remaindersArray[val] = pos; + val *= 10; + val %= i; + pos++; + } + + if (pos - remaindersArray[val] > sequenceSize) { + d = i; + sequenceSize = pos - remaindersArray[val]; + } + } + return d; + } +} From 5b9299ae537bbcb68d8611cd8856d7c7bcf77cbc Mon Sep 17 00:00:00 2001 From: daniel-campbell-327 <63069027+daniel-campbell-327@users.noreply.github.com> Date: Mon, 26 Apr 2021 17:17:44 +0100 Subject: [PATCH 8/8] Added Problem 15 --- ProjectEuler/Problem15.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ProjectEuler/Problem15.java diff --git a/ProjectEuler/Problem15.java b/ProjectEuler/Problem15.java new file mode 100644 index 000000000000..2cc3c608da1d --- /dev/null +++ b/ProjectEuler/Problem15.java @@ -0,0 +1,18 @@ +package ProjectEuler; + +public class Problem15 { + public static void main(String[] args) { + assert solution() == 137846528820L; + } + + public static long solution() { + long paths = 1; + + for (int i = 0; i < 20; i++) { + paths *= (2 * 20) - i; + paths /= i + 1; + } + + return paths; + } +}