remove `ProjectEuler`

上级 ea123111
......@@ -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)
......
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.
*
* <p>Find the sum of all the multiples of 3 or 5 below 1000.
*
* <p>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;
}
}
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:
*
* <p>1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* <p>By considering the terms in the Fibonacci sequence whose values do not exceed four million,
* find the sum of the even-valued terms.
*
* <p>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;
}
}
package ProjectEuler;
/**
* The prime factors of 13195 are 5, 7, 13 and 29.
*
* <p>What is the largest prime factor of the number 600851475143 ?
*
* <p>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;
}
}
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.
*
* <p>Find the largest palindrome made from the product of two 3-digit numbers.
*
* <p>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 */
}
}
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.
*
* <p>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;
}
}
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.
*
* <p>What is the 10 001st prime number?
*
* <p>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;
}
}
package ProjectEuler;
/**
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
*
* <p>a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
*
* <p>There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
*
* <p>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 */
}
}
package ProjectEuler;
/**
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
*
* <p>Find the sum of all the primes below two million.
*
* <p>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;
}
}
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:
*
* <p>1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
*
* <p>Let us list the factors of the first seven triangle numbers:
*
* <p>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.
*
* <p>What is the value of the first triangle number to have over five hundred divisors?
*
* <p>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;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册