GeezerVison is showing github.com — Added ProjectEuler · JavaInCloud/Java@cdd662c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +51Lines changed: 51 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package ProjectEuler; | | 2 | + | | 3 | +/** | | 4 | + * If we list all the natural numbers below 10 that are multiples of 3 or 5, | | 5 | + * we get 3, 5, 6 and 9. The sum of these multiples is 23. | | 6 | + * <p> | | 7 | + * Find the sum of all the multiples of 3 or 5 below 1000. | | 8 | + * <p> | | 9 | + * Link: https://projecteuler.net/problem=1 | | 10 | + */ | | 11 | +public class Problem01 { | | 12 | + public static void main(String[] args) { | | 13 | + int[][] testNumber = { | | 14 | + {3, 0}, | | 15 | + {4, 3}, | | 16 | + {10, 23}, | | 17 | + {1000, 233168}, | | 18 | + {-1, 0} | | 19 | + }; | | 20 | + | | 21 | + for (int[] ints : testNumber) { | | 22 | + assert solution1(ints[0]) == ints[1]; | | 23 | + assert solution2(ints[0]) == ints[1]; | | 24 | + } | | 25 | + } | | 26 | + | | 27 | + private static int solution1(int n) { | | 28 | + int sum = 0; | | 29 | + for (int i = 3; i < n; ++i) { | | 30 | + if (i % 3 == 0 || i % 5 == 0) { | | 31 | + sum += i; | | 32 | + } | | 33 | + } | | 34 | + return sum; | | 35 | + } | | 36 | + | | 37 | + private static int solution2(int n) { | | 38 | + int sum = 0; | | 39 | + | | 40 | + int terms = (n - 1) / 3; | | 41 | + sum += terms * (6 + (terms - 1) * 3) / 2; | | 42 | + | | 43 | + terms = (n - 1) / 5; | | 44 | + sum += terms * (10 + (terms - 1) * 5) / 2; | | 45 | + | | 46 | + terms = (n - 1) / 15; | | 47 | + sum -= terms * (30 + (terms - 1) * 15) / 2; | | 48 | + | | 49 | + return sum; | | 50 | + } | | 51 | +} |
Collapse file Copy file name to clipboard +43Lines changed: 43 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package ProjectEuler; | | 2 | + | | 3 | +/** | | 4 | + * Each new term in the Fibonacci sequence is generated by adding the previous two terms. | | 5 | + * By starting with 1 and 2, the first 10 terms will be: | | 6 | + * <p> | | 7 | + * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | | 8 | + * <p> | | 9 | + * By considering the terms in the Fibonacci sequence whose values do not exceed four million, | | 10 | + * find the sum of the even-valued terms. | | 11 | + * <p> | | 12 | + * Link: https://projecteuler.net/problem=2 | | 13 | + */ | | 14 | +public class Problem02 { | | 15 | + public static void main(String[] args) { | | 16 | + int[][] testNumbers = { | | 17 | + {10, 10}, /* 2 + 8 == 10 */ | | 18 | + {15, 10}, /* 2 + 8 == 10 */ | | 19 | + {2, 2}, | | 20 | + {1, 0}, | | 21 | + {89, 44} /* 2 + 8 + 34 == 44 */ | | 22 | + }; | | 23 | + | | 24 | + for (int[] ints : testNumbers) { | | 25 | + assert solution1(ints[0]) == ints[1]; | | 26 | + } | | 27 | + } | | 28 | + | | 29 | + private static int solution1(int n) { | | 30 | + int sum = 0; | | 31 | + int first = 1; | | 32 | + int second = 2; | | 33 | + while (second <= n) { | | 34 | + if (second % 2 == 0) { | | 35 | + sum += second; | | 36 | + } | | 37 | + int temp = first + second; | | 38 | + first = second; | | 39 | + second = temp; | | 40 | + } | | 41 | + return sum; | | 42 | + } | | 43 | +} |
|