Permalink
Browse files

modify constructors all class to require parameter

  • Loading branch information...
1 parent 811e4e3 commit ae065750fc392ad8e9396edfabbb21eed3cd9521 @sano66 committed Dec 28, 2011
@@ -15,15 +15,24 @@
*/
public class Problem001 {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 999;
+ @Deprecated
public Problem001() {
for (int i = 1; i < TARGET + 1; i++) {
if (i % 3 == 0 || i % 5 == 0)
answer += i;
}
}
+ public Problem001(int target) {
+ for (int i = 1; i < target + 1; i++) {
+ if (i % 3 == 0 || i % 5 == 0)
+ answer += i;
+ }
+ }
+
public int getAnswer() {
return answer;
}
@@ -15,13 +15,20 @@
*/
public class Problem001Alternate {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 999;
+ @Deprecated
public Problem001Alternate() {
answer = sumDivideBy(3, TARGET) + sumDivideBy(5, TARGET)
- sumDivideBy(15, TARGET);
}
+ public Problem001Alternate(int target) {
+ answer = sumDivideBy(3, target) + sumDivideBy(5, target)
+ - sumDivideBy(15, target);
+ }
+
private int sumDivideBy(int n, int target) {
int p = target / n;
return n * (1 + p) * p / 2;
@@ -18,8 +18,10 @@
*/
public class Problem002 {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 4000000;
+ @Deprecated
public Problem002() {
int n = 0;
for (int i = 1; (n = fibonacci(i)) < TARGET; i++) {
@@ -28,6 +30,14 @@ public Problem002() {
}
}
+ public Problem002(int target) {
+ int n = 0;
+ for (int i = 1; (n = fibonacci(i)) < target; i++) {
+ if (n % 2 == 0)
+ answer += n;
+ }
+ }
+
private int fibonacci(int n) {
if (n == 1)
return 1;
@@ -16,10 +16,12 @@
*/
public class Problem005 {
private int answer = 1;
+ @Deprecated
private static final int TARGET = 20;
private static final int[] multipleNumberArray = { 1, 2, 3, 2, 5, 1, 7, 2,
3, 1, 11, 1, 13, 1, 1, 2, 17, 1, 19, 1 };
+ @Deprecated
public Problem005() {
// answer = 1 * 2 * 3 * 2 * 5 * 1 * 7 * 2 * 3 * 1; // 2520
// answer *= 11 * 1 * 13 * 1 * 1 * 2 * 17 * 1 * 19 * 1; // 232792560
@@ -28,6 +30,16 @@ public Problem005() {
}
}
+ public Problem005(int target) {
+ // answer = 1 * 2 * 3 * 2 * 5 * 1 * 7 * 2 * 3 * 1; // 2520
+ // answer *= 11 * 1 * 13 * 1 * 1 * 2 * 17 * 1 * 19 * 1; // 232792560
+ if (target > 20)
+ throw new IllegalArgumentException(String.valueOf(target));
+ for (int i = 0; i < target; i++) {
+ answer *= multipleNumberArray[i];
+ }
+ }
+
public int getAnswer() {
return answer;
}
@@ -22,8 +22,10 @@
*/
public class Problem006 {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 100;
+ @Deprecated
public Problem006() {
int sum_square = 0;
for (int i = 1; i < TARGET + 1; i++) {
@@ -33,6 +35,15 @@ public Problem006() {
answer = (square_sum * square_sum) - sum_square;
}
+ public Problem006(int target) {
+ int sum_square = 0;
+ for (int i = 1; i < target + 1; i++) {
+ sum_square += (i * i);
+ }
+ int square_sum = (1 + target) * target / 2;
+ answer = (square_sum * square_sum) - sum_square;
+ }
+
public int getAnswer() {
return answer;
}
@@ -22,14 +22,22 @@
*/
public class Problem006Alternate {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 100;
+ @Deprecated
public Problem006Alternate() {
int sum_square = (2 * TARGET + 1) * (TARGET + 1) * TARGET / 6;
int square_sum = (1 + TARGET) * TARGET / 2;
answer = (square_sum * square_sum) - sum_square;
}
+ public Problem006Alternate(int target) {
+ int sum_square = (2 * target + 1) * (target + 1) * target / 6;
+ int square_sum = (1 + target) * target / 2;
+ answer = (square_sum * square_sum) - sum_square;
+ }
+
public int getAnswer() {
return answer;
}
@@ -15,15 +15,22 @@
*/
public class Problem007 {
private int answer = 0;
+ @Deprecated
private static final int TARGET = 10001;
private int[] primeNumberArray = new int[TARGET];
+ @Deprecated
public Problem007() {
for (int i = 0; i < TARGET; i++)
primeNumberArray[i] = getNextPrimeNumber(i);
answer = primeNumberArray[TARGET - 1];
}
+ public Problem007(int target) {
+ for (int i = 0; i < target; i++)
+ primeNumberArray[i] = getNextPrimeNumber(i);
+ answer = primeNumberArray[target - 1];
+ }
private int getNextPrimeNumber(int n) {
if (n == 0)
return 2;
@@ -9,11 +9,19 @@
public void testProblem001() {
//
assertEquals(233168, (new Problem001()).getAnswer());
+ //
+ assertEquals(23, (new Problem001(10 - 1)).getAnswer());
+ //
+ assertEquals(233168, (new Problem001(1000 - 1)).getAnswer());
}
@Test
public void testProblem001Alternate() {
//
assertEquals(233168, (new Problem001Alternate()).getAnswer());
+ //
+ assertEquals(23, (new Problem001Alternate(10 - 1)).getAnswer());
+ //
+ assertEquals(233168, (new Problem001Alternate(1000 - 1)).getAnswer());
}
}
@@ -9,5 +9,11 @@
public void testProblem002() {
//
assertEquals(4613732, (new Problem002()).getAnswer());
+ //
+ assertEquals(10, (new Problem002(9)).getAnswer());
+ //
+ assertEquals(44, (new Problem002(35)).getAnswer());
+ //
+ assertEquals(4613732, (new Problem002(4000000)).getAnswer());
}
}
@@ -9,5 +9,16 @@
public void testProblem005() {
//
assertEquals(232792560, (new Problem005()).getAnswer());
+ //
+ assertEquals(2520, (new Problem005(10)).getAnswer());
+ //
+ assertEquals(232792560, (new Problem005(20)).getAnswer());
+ //
+ try {
+ assertEquals(232792560, (new Problem005(21)).getAnswer());
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertEquals("21", e.getMessage());
+ }
}
}
@@ -9,12 +9,20 @@
public void testProblem006() {
//
assertEquals(25164150, (new Problem006()).getAnswer());
+ //
+ assertEquals(2640, (new Problem006(10)).getAnswer());
+ //
+ assertEquals(25164150, (new Problem006(100)).getAnswer());
}
@Test
public void testProblem006Alternate() {
//
assertEquals(25164150, (new Problem006Alternate()).getAnswer());
+ //
+ assertEquals(2640, (new Problem006Alternate(10)).getAnswer());
+ //
+ assertEquals(25164150, (new Problem006Alternate(100)).getAnswer());
}
}
@@ -9,5 +9,9 @@
public void testProblem007() {
//
assertEquals(104743, (new Problem007()).getAnswer());
+ //
+ assertEquals(13, (new Problem007(6)).getAnswer());
+ //
+ assertEquals(104743, (new Problem007(10001)).getAnswer());
}
}

0 comments on commit ae06575

Please sign in to comment.