diff --git a/src/main/java/net/projecteuler/problems/problems026to050/Problem028.java b/src/main/java/net/projecteuler/problems/problems026to050/Problem028.java new file mode 100644 index 0000000..19d4a70 --- /dev/null +++ b/src/main/java/net/projecteuler/problems/problems026to050/Problem028.java @@ -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 numbersInRightToLeftDiagonal(int count) { + var current = 1; + var increment = 2; + var storage = new HashSet(); + storage.add(current); + for (int i = 0; i < count - 1; i++) { + current += increment; + increment += 2; + storage.add(current); + } + return storage; + } + + private HashSet numbersInLeftToRightDiagonal(int count) { + var current = 1; + var increment = 4; + var storage = new HashSet(); + 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; + } +} diff --git a/src/test/java/net/projecteuler/problems/problems026to050/Problem028Test.java b/src/test/java/net/projecteuler/problems/problems026to050/Problem028Test.java new file mode 100644 index 0000000..ddc9bb5 --- /dev/null +++ b/src/test/java/net/projecteuler/problems/problems026to050/Problem028Test.java @@ -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()); + } +}