Skip to content

Commit 7b1e749

Browse files
committed
Problems 4-10
1 parent 8221981 commit 7b1e749

14 files changed

+352
-0
lines changed

src/main/java/Problem10.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Title: Summation of primes
3+
*
4+
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
5+
* Find the sum of all the primes below two million.
6+
*/
7+
public class Problem10 {
8+
public long solve() {
9+
long result = 0;
10+
11+
for (int i = 2; i < 2000000; i++) {
12+
if (isPrime(i)) {
13+
result += i;
14+
}
15+
}
16+
return result;
17+
}
18+
19+
private boolean isPrime(int num) {
20+
for (int i = 2; i <= Math.sqrt(num); i++) {
21+
if (num % i == 0) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}
27+
28+
public static void main(String[] args) {
29+
long result = new Problem10().solve();
30+
System.out.println("Result: " + result);
31+
}
32+
}

src/main/java/Problem4.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Title: Largest palindrome product
3+
*
4+
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
5+
* Find the largest palindrome made from the product of two 3-digit numbers.
6+
*/
7+
public class Problem4 {
8+
public int solve() {
9+
int result = -1;
10+
11+
for (int num1 = 999; num1 > 99; num1--) {
12+
for (int num2 = 999; num2 > 99; num2--) {
13+
int product = num1 * num2;
14+
if (isPalindromic(product) && product > result) {
15+
result = product;
16+
}
17+
}
18+
}
19+
return result;
20+
}
21+
22+
private boolean isPalindromic(int num) {
23+
String str = String.valueOf(num);
24+
for (int i = 0; i < str.length() / 2; i++) {
25+
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
}
31+
32+
public static void main(String[] args) {
33+
int result = new Problem4().solve();
34+
System.out.println("Result: " + result);
35+
}
36+
}

src/main/java/Problem5.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
/**
5+
* Title: Smallest multiple
6+
*
7+
* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
8+
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
9+
*/
10+
public class Problem5 {
11+
public int solve() {
12+
Map<Integer, Integer> factors = new HashMap<>();
13+
14+
for (int i = 2; i <= 20; i++) {
15+
Map<Integer, Integer> factorsForNum = getFactors(i);
16+
for (Map.Entry<Integer, Integer> entry : factorsForNum.entrySet()) {
17+
Integer oldExponent = factors.get(entry.getKey());
18+
if (oldExponent == null || entry.getValue() > oldExponent) {
19+
factors.put(entry.getKey(), entry.getValue());
20+
}
21+
}
22+
}
23+
int result = 1;
24+
for (Map.Entry<Integer, Integer> entry : factors.entrySet()) {
25+
result *= Math.pow(entry.getKey(), entry.getValue());
26+
}
27+
return result;
28+
}
29+
30+
private Map<Integer, Integer> getFactors(int num) {
31+
int copy = num;
32+
Map<Integer, Integer> result = new HashMap<>();
33+
int[] primeNumbers = new int[]{2, 3, 5, 7, 11, 13, 17, 19};
34+
35+
for (int i : primeNumbers) {
36+
if (copy % i == 0) {
37+
int exponent = (int)(Math.log(copy) / Math.log(i));
38+
result.put(i, exponent);
39+
}
40+
}
41+
return result;
42+
}
43+
44+
public static void main(String[] args) {
45+
// 1 2 3 4 5 6 7 8 9 10 -> 2,3,2^2,5,2*3,7,2^3,3^2,2*5
46+
// 2^3,3^2,5,7 -> 8*9*5*7 = 2520
47+
int result = new Problem5().solve();
48+
System.out.println("Result: " + result);
49+
}
50+
}

src/main/java/Problem6.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Title: Sum square difference
3+
*
4+
* The sum of the squares of the first ten natural numbers is,
5+
* 1^2 + 2^2 + ... + 10^2 = 385
6+
* The square of the sum of the first ten natural numbers is,
7+
* (1 + 2 + ... + 10)^2 = 552 = 3025
8+
* 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.
9+
* Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
10+
*/
11+
public class Problem6 {
12+
public int solve() {
13+
int sum = 0;
14+
int squareSum = 0;
15+
16+
for (int i = 1; i <= 100; i++) {
17+
sum += i;
18+
squareSum += i*i;
19+
}
20+
return sum * sum - squareSum;
21+
}
22+
23+
public static void main(String[] args) {
24+
int result = new Problem6().solve();
25+
System.out.println("Result: " + result);
26+
}
27+
}

src/main/java/Problem7.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Title: 10001st prime
3+
*
4+
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
5+
* What is the 10001st prime number?
6+
*/
7+
public class Problem7 {
8+
public int solve() {
9+
int primeCount = 0;
10+
int current = 1;
11+
while (primeCount < 10001) {
12+
current++;
13+
if (isPrime(current)) {
14+
primeCount++;
15+
}
16+
}
17+
return current;
18+
}
19+
20+
private boolean isPrime(int num) {
21+
for (int i = 2; i <= Math.sqrt(num); i++) {
22+
if (num % i == 0) {
23+
return false;
24+
}
25+
}
26+
return true;
27+
}
28+
29+
public static void main(String[] args) {
30+
int result = new Problem7().solve();
31+
System.out.println("Result: " + result);
32+
}
33+
}

src/main/java/Problem8.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Title: Largest product in a series
3+
*
4+
* The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
5+
* 73167176531330624919225119674426574742355349194934
6+
* 96983520312774506326239578318016984801869478851843
7+
* 85861560789112949495459501737958331952853208805511
8+
* 12540698747158523863050715693290963295227443043557
9+
* 66896648950445244523161731856403098711121722383113
10+
* 62229893423380308135336276614282806444486645238749
11+
* 30358907296290491560440772390713810515859307960866
12+
* 70172427121883998797908792274921901699720888093776
13+
* 65727333001053367881220235421809751254540594752243
14+
* 52584907711670556013604839586446706324415722155397
15+
* 53697817977846174064955149290862569321978468622482
16+
* 83972241375657056057490261407972968652414535100474
17+
* 82166370484403199890008895243450658541227588666881
18+
* 16427171479924442928230863465674813919123162824586
19+
* 17866458359124566529476545682848912883142607690042
20+
* 24219022671055626321111109370544217506941658960408
21+
* 07198403850962455444362981230987879927244284909188
22+
* 84580156166097919133875499200524063689912560717606
23+
* 05886116467109405077541002256983155200055935729725
24+
* 71636269561882670428252483600823257530420752963450
25+
* Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
26+
*/
27+
public class Problem8 {
28+
final String input =
29+
"73167176531330624919225119674426574742355349194934"+
30+
"96983520312774506326239578318016984801869478851843"+
31+
"85861560789112949495459501737958331952853208805511"+
32+
"12540698747158523863050715693290963295227443043557"+
33+
"66896648950445244523161731856403098711121722383113"+
34+
"62229893423380308135336276614282806444486645238749"+
35+
"30358907296290491560440772390713810515859307960866"+
36+
"70172427121883998797908792274921901699720888093776"+
37+
"65727333001053367881220235421809751254540594752243"+
38+
"52584907711670556013604839586446706324415722155397"+
39+
"53697817977846174064955149290862569321978468622482"+
40+
"83972241375657056057490261407972968652414535100474"+
41+
"82166370484403199890008895243450658541227588666881"+
42+
"16427171479924442928230863465674813919123162824586"+
43+
"17866458359124566529476545682848912883142607690042"+
44+
"24219022671055626321111109370544217506941658960408"+
45+
"07198403850962455444362981230987879927244284909188"+
46+
"84580156166097919133875499200524063689912560717606"+
47+
"05886116467109405077541002256983155200055935729725"+
48+
"71636269561882670428252483600823257530420752963450";
49+
50+
public long solve() {
51+
long result = 0;
52+
char[] chars = input.toCharArray();
53+
for (int i = 0; i < chars.length - 13; i++) {
54+
long product = calculate13Product(i, chars);
55+
if (product > result) {
56+
result = product;
57+
}
58+
}
59+
return result;
60+
}
61+
62+
private long calculate13Product(int startPosition, char[] arr) {
63+
long result = 1;
64+
for (int i = startPosition; i < startPosition + 13; i++) {
65+
result *= arr[i] - '0'; // char to int
66+
}
67+
return result;
68+
}
69+
70+
public static void main(String[] args) {
71+
long result = new Problem8().solve();
72+
System.out.println("Result: " + result);
73+
}
74+
}

src/main/java/Problem9.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Title: Special Pythagorean triplet
3+
*
4+
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
5+
* a^2 + b^2 = c^2
6+
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
7+
* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
8+
* Find the product abc.
9+
*/
10+
public class Problem9 {
11+
public int solve() {
12+
13+
for (int a = 1; a < 1000; a++) {
14+
for (int b = a + 1; b < 1000; b++) {
15+
int cSquare = a*a + b*b;
16+
double c = Math.sqrt(cSquare);
17+
18+
if (c - (int)c == 0 && a+b+c == 1000) {
19+
return a*b*(int)c;
20+
}
21+
}
22+
}
23+
return -1;
24+
}
25+
26+
public static void main(String[] args) {
27+
int result = new Problem9().solve();
28+
System.out.println("Result: " + result);
29+
}
30+
}

src/test/java/Problem10Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem10Test {
5+
@Test
6+
public void checkValidValue() {
7+
long result = new Problem10().solve();
8+
Assertions.assertThat(result).isEqualTo(142913828922L);
9+
}
10+
}

src/test/java/Problem4Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem4Test {
5+
@Test
6+
public void checkValidValue() {
7+
int result = new Problem4().solve();
8+
Assertions.assertThat(result).isEqualTo(906609);
9+
}
10+
}

src/test/java/Problem5Test.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import org.assertj.core.api.Assertions;
2+
import org.junit.Test;
3+
4+
public class Problem5Test {
5+
@Test
6+
public void checkValidValue() {
7+
int result = new Problem5().solve();
8+
Assertions.assertThat(result).isEqualTo(232792560);
9+
}
10+
}

0 commit comments

Comments
 (0)