Bonus challenge for week 4
This commit is contained in:
+15
-2
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user