空穴問題是關於你如何解決這個問題(IMHO)
若干X可以植根於基Ñ如果:
的的x ^(1/n的)是整數,其中n> 0
因此該方法可以是:
public static void main(final String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(" has " + i + " exactly a root base2(square)? " + isRootInteger(i, 2));
System.out.println(" has " + i + " exactly a root base3(cubic)? " + isRootInteger(i, 3));
}
}
public static boolean isRootInteger(int number, int root) {
double dResult = Math.pow(number, 1.0/root);
if ((dResult == Math.floor(dResult)) && !Double.isInfinite(dResult)) {
return true;
}
return false;
}
''n'的第n個根可以由'Math.pow(x,1.0/n)'給出。 – khelwood
這是一個數學問題,而不是一個Java,投票結束,而不是適當的地方 – azro
這是一個比java更多的數學問題。你可以做Math.pow(a,1.0/p),其中p是你想要的權力和double類型的權力。 2,3,... etc – Multithreader