From 4ba52c1c838521d587acdf241086dddc9a1d2253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Mon, 10 May 2021 18:50:56 +0200 Subject: [PATCH] remove `ProjectEuler` --- DIRECTORY.md | 11 ------- ProjectEuler/Problem01.java | 51 ------------------------------ ProjectEuler/Problem02.java | 43 ------------------------- ProjectEuler/Problem03.java | 62 ------------------------------------- ProjectEuler/Problem04.java | 41 ------------------------ ProjectEuler/Problem06.java | 42 ------------------------- ProjectEuler/Problem07.java | 60 ----------------------------------- ProjectEuler/Problem09.java | 28 ----------------- ProjectEuler/Problem10.java | 55 -------------------------------- ProjectEuler/Problem12.java | 54 -------------------------------- 10 files changed, 447 deletions(-) delete mode 100644 ProjectEuler/Problem01.java delete mode 100644 ProjectEuler/Problem02.java delete mode 100644 ProjectEuler/Problem03.java delete mode 100644 ProjectEuler/Problem04.java delete mode 100644 ProjectEuler/Problem06.java delete mode 100644 ProjectEuler/Problem07.java delete mode 100644 ProjectEuler/Problem09.java delete mode 100644 ProjectEuler/Problem10.java delete mode 100644 ProjectEuler/Problem12.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 537f0ff1..b787f26e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -220,17 +220,6 @@ * [TwoPointers](https://github.com/TheAlgorithms/Java/blob/master/Others/TwoPointers.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) - * [Problem03](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem03.java) - * [Problem04](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem04.java) - * [Problem06](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem06.java) - * [Problem07](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem07.java) - * [Problem09](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem09.java) - * [Problem10](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem10.java) - * [Problem12](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem12.java) - ## Searches * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) * [InterpolationSearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/InterpolationSearch.java) diff --git a/ProjectEuler/Problem01.java b/ProjectEuler/Problem01.java deleted file mode 100644 index a19876f8..00000000 --- a/ProjectEuler/Problem01.java +++ /dev/null @@ -1,51 +0,0 @@ -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; - } -} diff --git a/ProjectEuler/Problem02.java b/ProjectEuler/Problem02.java deleted file mode 100644 index 2d67c54f..00000000 --- a/ProjectEuler/Problem02.java +++ /dev/null @@ -1,43 +0,0 @@ -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; - } -} diff --git a/ProjectEuler/Problem03.java b/ProjectEuler/Problem03.java deleted file mode 100644 index 31d6fdc4..00000000 --- a/ProjectEuler/Problem03.java +++ /dev/null @@ -1,62 +0,0 @@ -package ProjectEuler; - -/** - * The prime factors of 13195 are 5, 7, 13 and 29. - * - *

What is the largest prime factor of the number 600851475143 ? - * - *

Link: https://projecteuler.net/problem=3 - */ -public class Problem03 { - - /** - * Checks if a number is prime or not - * - * @param n the number - * @return {@code true} if {@code n} is prime - */ - public static boolean isPrime(int n) { - if (n == 2) { - return true; - } - if (n < 2 || n % 2 == 0) { - return false; - } - for (int i = 3, limit = (int) Math.sqrt(n); i <= limit; i += 2) { - if (n % i == 0) { - return false; - } - } - return true; - } - - /** - * Calculate all the prime factors of a number and return the largest - * - * @param n integer number - * @return the maximum prime factor of the given number - */ - static long maxPrimeFactor(long n) { - for (int i = 2; i < n / 2; i++) { - if (isPrime(i)) - while (n % i == 0) { - n /= i; - } - } - return n; - } - - public static void main(String[] args) { - int[][] testNumbers = { - {87, 29}, - {10, 5}, - {21, 7}, - {657, 73}, - {777, 37} - }; - for (int[] num : testNumbers) { - assert Problem03.maxPrimeFactor(num[0]) == num[1]; - } - assert Problem03.maxPrimeFactor(600851475143L) == 6857; - } -} diff --git a/ProjectEuler/Problem04.java b/ProjectEuler/Problem04.java deleted file mode 100644 index 9f9a84bd..00000000 --- a/ProjectEuler/Problem04.java +++ /dev/null @@ -1,41 +0,0 @@ -package ProjectEuler; - -/** - * A palindromic number reads the same both ways. The largest palindrome made from the product of - * two 2-digit numbers is 9009 = 91 × 99. - * - *

Find the largest palindrome made from the product of two 3-digit numbers. - * - *

link: https://projecteuler.net/problem=4 - */ -public class Problem04 { - public static void main(String[] args) { - - assert solution1(10000) == -1; - assert solution1(20000) == 19591; /* 19591 == 143*137 */ - assert solution1(30000) == 29992; /* 29992 == 184*163 */ - assert solution1(40000) == 39893; /* 39893 == 287*139 */ - assert solution1(50000) == 49894; /* 49894 == 494*101 */ - assert solution1(60000) == 59995; /* 59995 == 355*169 */ - assert solution1(70000) == 69996; /* 69996 == 614*114 */ - assert solution1(80000) == 79897; /* 79897 == 733*109 */ - assert solution1(90000) == 89798; /* 89798 == 761*118 */ - assert solution1(100000) == 99999; /* 100000 == 813*123 */ - } - - private static int solution1(int n) { - for (int i = n - 1; i >= 10000; --i) { - String strNumber = String.valueOf(i); - - /* Test if strNumber is palindrome */ - if (new StringBuilder(strNumber).reverse().toString().equals(strNumber)) { - for (int divisor = 999; divisor >= 100; --divisor) { - if (i % divisor == 0 && String.valueOf(i / divisor).length() == 3) { - return i; - } - } - } - } - return -1; /* not found */ - } -} diff --git a/ProjectEuler/Problem06.java b/ProjectEuler/Problem06.java deleted file mode 100644 index 0c1fbecd..00000000 --- a/ProjectEuler/Problem06.java +++ /dev/null @@ -1,42 +0,0 @@ -package ProjectEuler; - -/** - * The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385 The - * square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 552 = 3025 Hence - * the difference between the sum of the squares of the first ten natural numbers and the square of - * the sum is 3025 − 385 = 2640. Find the difference between the sum of the squares of the first N - * natural numbers and the square of the sum. - * - *

link: https://projecteuler.net/problem=6 - */ -public class Problem06 { - public static void main(String[] args) { - int[][] testNumbers = { - {10, 2640}, - {15, 13160}, - {20, 41230}, - {50, 1582700} - }; - - for (int[] testNumber : testNumbers) { - assert solution1(testNumber[0]) == testNumber[1] - && solutions2(testNumber[0]) == testNumber[1]; - } - } - - private static int solution1(int n) { - int sum1 = 0; - int sum2 = 0; - for (int i = 1; i <= n; ++i) { - sum1 += i * i; - sum2 += i; - } - return sum2 * sum2 - sum1; - } - - private static int solutions2(int n) { - int sumOfSquares = n * (n + 1) * (2 * n + 1) / 6; - int squareOfSum = (int) Math.pow((n * (n + 1) / 2.0), 2); - return squareOfSum - sumOfSquares; - } -} diff --git a/ProjectEuler/Problem07.java b/ProjectEuler/Problem07.java deleted file mode 100644 index 25df143c..00000000 --- a/ProjectEuler/Problem07.java +++ /dev/null @@ -1,60 +0,0 @@ -package ProjectEuler; - -/** - * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is - * 13. - * - *

What is the 10 001st prime number? - * - *

link: https://projecteuler.net/problem=7 - */ -public class Problem07 { - public static void main(String[] args) { - int[][] testNumbers = { - {1, 2}, - {2, 3}, - {3, 5}, - {4, 7}, - {5, 11}, - {6, 13}, - {20, 71}, - {50, 229}, - {100, 541} - }; - for (int[] number : testNumbers) { - assert solution1(number[0]) == number[1]; - } - } - - /*** - * Checks if a number is prime or not - * @param number the number - * @return {@code true} if {@code number} is prime - */ - private static boolean isPrime(int number) { - if (number == 2) { - return true; - } - if (number < 2 || number % 2 == 0) { - return false; - } - for (int i = 3, limit = (int) Math.sqrt(number); i <= limit; i += 2) { - if (number % i == 0) { - return false; - } - } - return true; - } - - private static int solution1(int n) { - int count = 0; - int number = 1; - - while (count != n) { - if (isPrime(++number)) { - count++; - } - } - return number; - } -} diff --git a/ProjectEuler/Problem09.java b/ProjectEuler/Problem09.java deleted file mode 100644 index b0dbebb9..00000000 --- a/ProjectEuler/Problem09.java +++ /dev/null @@ -1,28 +0,0 @@ -package ProjectEuler; - -/** - * A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, - * - *

a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. - * - *

There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. - * - *

link: https://projecteuler.net/problem=9 - */ -public class Problem09 { - public static void main(String[] args) { - assert solution1() == 31875000; - } - - private static int solution1() { - for (int i = 0; i <= 300; ++i) { - for (int j = 0; j <= 400; ++j) { - int k = 1000 - i - j; - if (i * i + j * j == k * k) { - return i * j * k; - } - } - } - return -1; /* should not happen */ - } -} diff --git a/ProjectEuler/Problem10.java b/ProjectEuler/Problem10.java deleted file mode 100644 index a51a2228..00000000 --- a/ProjectEuler/Problem10.java +++ /dev/null @@ -1,55 +0,0 @@ -package ProjectEuler; - -/** - * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. - * - *

Find the sum of all the primes below two million. - * - *

link: https://projecteuler.net/problem=10 - */ -public class Problem10 { - public static void main(String[] args) { - long[][] testNumbers = { - {2000000, 142913828922L}, - {10000, 5736396}, - {5000, 1548136}, - {1000, 76127}, - {10, 17}, - {7, 10} - }; - - for (long[] testNumber : testNumbers) { - assert solution1(testNumber[0]) == testNumber[1]; - } - } - - /*** - * Checks if a number is prime or not - * @param n the number - * @return {@code true} if {@code n} is prime - */ - private static boolean isPrime(int n) { - if (n == 2) { - return true; - } - if (n < 2 || n % 2 == 0) { - return false; - } - for (int i = 3, limit = (int) Math.sqrt(n); i <= limit; i += 2) { - if (n % i == 0) { - return false; - } - } - return true; - } - - private static long solution1(long n) { - long sum = 0; - for (int i = 2; i < n; ++i) { - if (isPrime(i)) { - sum += i; - } - } - return sum; - } -} diff --git a/ProjectEuler/Problem12.java b/ProjectEuler/Problem12.java deleted file mode 100644 index 9743bc9d..00000000 --- a/ProjectEuler/Problem12.java +++ /dev/null @@ -1,54 +0,0 @@ -package ProjectEuler; -/** - * The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle - * number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: - * - *

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... - * - *

Let us list the factors of the first seven triangle numbers: - * - *

1: 1 3: 1,3 6: 1,2,3,6 10: 1,2,5,10 15: 1,3,5,15 21: 1,3,7,21 28: 1,2,4,7,14,28 We can see - * that 28 is the first triangle number to have over five divisors. - * - *

What is the value of the first triangle number to have over five hundred divisors? - * - *

link: https://projecteuler.net/problem=12 - */ -public class Problem12 { - - /** Driver Code */ - public static void main(String[] args) { - assert solution1(500) == 76576500; - } - - /* returns the nth triangle number; that is, the sum of all the natural numbers less than, or equal to, n */ - public static int triangleNumber(int n) { - int sum = 0; - for (int i = 0; i <= n; i++) sum += i; - return sum; - } - - public static int solution1(int number) { - int j = 0; // j represents the jth triangle number - int n = 0; // n represents the triangle number corresponding to j - int numberOfDivisors = 0; // number of divisors for triangle number n - - while (numberOfDivisors <= number) { - - // resets numberOfDivisors because it's now checking a new triangle number - // and also sets n to be the next triangle number - numberOfDivisors = 0; - j++; - n = triangleNumber(j); - - // for every number from 1 to the square root of this triangle number, - // count the number of divisors - for (int i = 1; i <= Math.sqrt(n); i++) if (n % i == 0) numberOfDivisors++; - - // 1 to the square root of the number holds exactly half of the divisors - // so multiply it by 2 to include the other corresponding half - numberOfDivisors *= 2; - } - return n; - } -}