-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerfulDigitCounts.java
More file actions
33 lines (26 loc) · 868 Bytes
/
PowerfulDigitCounts.java
File metadata and controls
33 lines (26 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package dev;
public class PowerfulDigitCounts {
public static void main(String[] args) {
int overallCount = 0;
int n = 1;
int count;
while ((count = countNDigitNthPowers(n)) > 0) {
overallCount += count;
n++;
}
System.out.println("The overall count of n-digit nth powers is: " + overallCount);
}
private static int countNDigitNthPowers(int n) {
// Lower bound for nth root of 10^(n-1)
int lowerBound = (int) Math.ceil(Math.pow(10, (n - 1) / (double) n));
// Upper bound is 10 - 1, because 10^n has n+1 digits
int upperBound = 10 - 1;
int count = 0;
for (int x = lowerBound; x <= upperBound; x++) {
if (Math.pow(x, n) < Math.pow(10, n)) {
count++;
}
}
return count;
}
}