Created
August 11, 2020 00:37
-
-
Save noel-yap/1f5bbd199c5cec6b6b4703ee23d45423 to your computer and use it in GitHub Desktop.
Distinct powers: https://projecteuler.net/problem=29
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
class Entry { | |
public final long base; | |
public final long exponent; | |
public Entry(long base, long exponent) { | |
// 2⁷ > 100 so we start from the sixth power | |
for (int e = 6; e > 1; --e) { | |
final double pow = Math.pow(base, 1.0 / e); | |
final long roundedPow = Math.round(pow); | |
// Normalize the base and exponent | |
if (pow == roundedPow) { | |
base = roundedPow; | |
exponent *= e; | |
break; | |
} | |
} | |
this.base = base; | |
this.exponent = exponent; | |
} | |
} | |
public class Solution { | |
public int distinctPowers() { | |
final Set<Entry> entries = new HashSet<Entry>(); | |
for (int b = 2; b < 101; ++b) { | |
for (int e = 2; e < 101; ++e) { | |
final Entry entry = new Entry(b, e); | |
entries.add(entry); | |
} | |
} | |
return entries.size(); | |
} | |
@Test | |
public void eyeballTest() { | |
System.out.println(distinctPowers()); | |
} | |
public static void main(String[] args) { | |
JUnitCore.main("Solution"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment