-
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
48 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/net/projecteuler/problems/problems026to050/Problem030.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,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(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/net/projecteuler/problems/problems026to050/Problem030Test.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 Problem030Test { | ||
|
||
@Test | ||
void solve() { | ||
assertEquals(443839, new Problem030().solve()); | ||
} | ||
} |