From 106f4b17c390c9bd87ffc1e06b66d5d672687923 Mon Sep 17 00:00:00 2001 From: Josh Ashton Date: Wed, 27 Sep 2023 13:35:59 -0600 Subject: [PATCH] Bonus challenge for week 4 --- Week2.java | 17 +++++++++++++++-- Week4Bonus.java | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 Week4Bonus.java diff --git a/Week2.java b/Week2.java index 06af0f9..c99e931 100644 --- a/Week2.java +++ b/Week2.java @@ -12,14 +12,27 @@ public class Week2 { * HINT: Solution 1 uses 1000 iterations in 1 loop. */ public static int problem1() { - return -1; // TODO + int sum = 0; + + for(int i = 0; i < 1000; i++) { + if(i % 5 == 0 || i % 3 == 0) sum += i; + } + + return sum; } /** * BONUS CHALLENGE: Find a more efficient solution for problem 1 (ie. a solution that uses less than 1000 iterations). */ public static int problem2() { - return -1; // TODO + int sum = 0; + for(int i = 5; i < 1000; i+=5) + sum += i; + + for(int i = 3; i < 1000; i+=3) + if(i % 5 != 0) sum += i; + + return sum; } /** diff --git a/Week4Bonus.java b/Week4Bonus.java new file mode 100644 index 0000000..e40b19a --- /dev/null +++ b/Week4Bonus.java @@ -0,0 +1,26 @@ +/** + * In this week's club bonus challenge, you will be tasked with solving grid traversal problem. + */ +public class Week4 { + + /** + * In a 2x2 grid, there are exactly 6 routes from the top-left corner to the bottom-right corner. + * Find an algorithm to find the number of routes in a 20x20 grid. + * + * Credit: Project Euler Problem 15. + * + * HINT: Recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion, recursion... + */ + public static long problem1() { + return -1; // TODO + } + + /** + * Test driver should not be edited! + */ + public static void main(String[] args) { + long ans1 = problem1(); + + System.out.println(ans1 == 137846528820 ? "Problem 1 is correct!" : "Problem 1 (" + ans1 + ") is not correct..."); + } +}