Skip to content

Commit

Permalink
Problem 30
Browse files Browse the repository at this point in the history
  • Loading branch information
plagov committed Jan 24, 2024
1 parent 9348872 commit b71c06a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.projecteuler.problems.problems026to050;

import java.util.stream.IntStream;

public class Problem030 {

private static final int POWER = 5;

private static int sumOfFifthPowers(int number) {
return Integer.toString(number).chars()
.map(Character::getNumericValue)
.map(i -> (int) Math.pow(i, 5))
.sum();
}

private static int getBound(int power, int base) {
var number = power * (int) Math.pow(base, 5);
var maxLength = Integer.toString(number).length();

while (Integer.toString(number).length() <= maxLength) {
power++;
number = power * (int) Math.pow(base, 5);
}
return number;
}

public int solve() {
var start = getBound(POWER, 1);
var end = getBound(POWER, 9);

return IntStream.rangeClosed(start, end)
.filter(number -> number == sumOfFifthPowers(number))
.sum();
}
}
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 Problem030Test {

@Test
void solve() {
assertEquals(443839, new Problem030().solve());
}
}

0 comments on commit b71c06a

Please sign in to comment.