-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/net/projecteuler/problems/problems026to050/Problem028.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package net.projecteuler.problems.problems026to050; | ||
|
||
import java.util.HashSet; | ||
|
||
public class Problem028 { | ||
|
||
public int solve() { | ||
var count = 1001; | ||
var numbers = numbersInLeftToRightDiagonal(count); | ||
numbers.addAll(numbersInRightToLeftDiagonal(count)); | ||
return numbers.stream().mapToInt(Integer::intValue).sum(); | ||
} | ||
|
||
private HashSet<Integer> numbersInRightToLeftDiagonal(int count) { | ||
var current = 1; | ||
var increment = 2; | ||
var storage = new HashSet<Integer>(); | ||
storage.add(current); | ||
for (int i = 0; i < count - 1; i++) { | ||
current += increment; | ||
increment += 2; | ||
storage.add(current); | ||
} | ||
return storage; | ||
} | ||
|
||
private HashSet<Integer> numbersInLeftToRightDiagonal(int count) { | ||
var current = 1; | ||
var increment = 4; | ||
var storage = new HashSet<Integer>(); | ||
storage.add(current); | ||
for (int i = 0; i < (count / 2); i++) { | ||
for (int k = 0; k < 2; k++) { | ||
current += increment; | ||
storage.add(current); | ||
} | ||
increment += 4; | ||
} | ||
return storage; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/net/projecteuler/problems/problems026to050/Problem028Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.projecteuler.problems.problems026to050; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class Problem028Test { | ||
|
||
@Test | ||
void solve() { | ||
assertEquals(669171001, new Problem028().solve()); | ||
} | ||
} |