Skip to content

Commit

Permalink
Solve Problem 28
Browse files Browse the repository at this point in the history
  • Loading branch information
plagov committed Mar 15, 2023
1 parent 7ea1ac3 commit 3583515
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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;
}
}
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());
}
}

0 comments on commit 3583515

Please sign in to comment.