From cdd662c9a1f05d1f8ee6f3c212e6ab139407f7d0 Mon Sep 17 00:00:00 2001 From: shellhub Date: Sun, 13 Sep 2020 17:23:51 +0800 Subject: [PATCH 1/2] Added ProjectEuler --- ProjectEuler/Problem01.java | 51 +++++++++++++++++++++++++++++++++++++ ProjectEuler/Problem02.java | 43 +++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 ProjectEuler/Problem01.java create mode 100644 ProjectEuler/Problem02.java diff --git a/ProjectEuler/Problem01.java b/ProjectEuler/Problem01.java new file mode 100644 index 000000000..fffb0eca7 --- /dev/null +++ b/ProjectEuler/Problem01.java @@ -0,0 +1,51 @@ +package ProjectEuler; + +/** + * If we list all the natural numbers below 10 that are multiples of 3 or 5, + * we get 3, 5, 6 and 9. The sum of these multiples is 23. + *

+ * Find the sum of all the multiples of 3 or 5 below 1000. + *

+ * Link: https://projecteuler.net/problem=1 + */ +public class Problem01 { + public static void main(String[] args) { + int[][] testNumber = { + {3, 0}, + {4, 3}, + {10, 23}, + {1000, 233168}, + {-1, 0} + }; + + for (int[] ints : testNumber) { + assert solution1(ints[0]) == ints[1]; + assert solution2(ints[0]) == ints[1]; + } + } + + private static int solution1(int n) { + int sum = 0; + for (int i = 3; i < n; ++i) { + if (i % 3 == 0 || i % 5 == 0) { + sum += i; + } + } + return sum; + } + + private static int solution2(int n) { + int sum = 0; + + int terms = (n - 1) / 3; + sum += terms * (6 + (terms - 1) * 3) / 2; + + terms = (n - 1) / 5; + sum += terms * (10 + (terms - 1) * 5) / 2; + + terms = (n - 1) / 15; + sum -= terms * (30 + (terms - 1) * 15) / 2; + + return sum; + } +} \ No newline at end of file diff --git a/ProjectEuler/Problem02.java b/ProjectEuler/Problem02.java new file mode 100644 index 000000000..5f8d1f051 --- /dev/null +++ b/ProjectEuler/Problem02.java @@ -0,0 +1,43 @@ +package ProjectEuler; + +/** + * Each new term in the Fibonacci sequence is generated by adding the previous two terms. + * By starting with 1 and 2, the first 10 terms will be: + *

+ * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... + *

+ * By considering the terms in the Fibonacci sequence whose values do not exceed four million, + * find the sum of the even-valued terms. + *

+ * Link: https://projecteuler.net/problem=2 + */ +public class Problem02 { + public static void main(String[] args) { + int[][] testNumbers = { + {10, 10}, /* 2 + 8 == 10 */ + {15, 10}, /* 2 + 8 == 10 */ + {2, 2}, + {1, 0}, + {89, 44} /* 2 + 8 + 34 == 44 */ + }; + + for (int[] ints : testNumbers) { + assert solution1(ints[0]) == ints[1]; + } + } + + private static int solution1(int n) { + int sum = 0; + int first = 1; + int second = 2; + while (second <= n) { + if (second % 2 == 0) { + sum += second; + } + int temp = first + second; + first = second; + second = temp; + } + return sum; + } +} \ No newline at end of file From fb36493dab4c3efdf5f54249ebcf93e1f6771a54 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 13 Sep 2020 09:24:23 +0000 Subject: [PATCH 2/2] updating DIRECTORY.md --- DIRECTORY.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 430940f52..c796ea9cb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -200,6 +200,10 @@ * [TowerOfHanoi](https://github.com/TheAlgorithms/Java/blob/master/Others/TowerOfHanoi.java) * [WorstFit](https://github.com/TheAlgorithms/Java/blob/master/Others/WorstFit.java) +## ProjectEuler + * [Problem01](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem01.java) + * [Problem02](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem02.java) + ## Searches * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) * [InterpolationSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/InterpolationSearch.java)