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