Skip to content

Instantly share code, notes, and snippets.

@imambungo
Last active October 14, 2019 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imambungo/f5c372ac2df1bbdd3baf1ba5e5cc9608 to your computer and use it in GitHub Desktop.
Save imambungo/f5c372ac2df1bbdd3baf1ba5e5cc9608 to your computer and use it in GitHub Desktop.
Calculating the nth Root in Java https://github.com/eugenp/tutorials/issues/6806
import java.lang.Math;
public class Main
{
public static void main(String[] args)
{
nthRootCalculatorTest(10000, 8);
}
public static Double calculateNthRootUsingMathLog(Double base, Double n)
{
return Math.pow(Math.E, Math.log(base)/n);
}
public static double calculateNthRootUsingMathPow(double base, double n)
{
return Math.pow(base, 1.0/n);
}
public static void nthRootCalculatorTest(double maxBase, double maxN)
{
int bothNotAccurate = 0;
int bothAccurate = 0;
int calculateUsingMathLogWin = 0;
int calculateUsingMathPowWin = 0;
String bothWrong = "both WRONG:\n\n";
String bothCorrect = "both CORRECT:\n\n";
String mathLogWin = "Math.log WIN:\n\n";
String mathPowWin = "Math.pow WIN:\n\n";
for (double i = 2; i <= maxN; i++)
{
for (double j = 2; j <= maxBase; j++)
{
double mathLogResult = calculateNthRootUsingMathLog(j, i);
double mathPowResult = calculateNthRootUsingMathPow(j, i);
// look for whole numbers
if (mathPowResult % 1 > 0.99999999 ||
mathPowResult % 1 < 0.00000001)
{
boolean mathLogIsCorrect = Math.pow(mathLogResult, i) == j;
boolean mathPowIsCorrect = Math.pow(mathPowResult, i) == j;
String result = "The " + i + " root of " + j + " equals to:\n"
+ "\tMath.log : " + mathLogResult + "\n"
+ "\tMath.pow : " + mathPowResult + "\n\n";
if (mathLogIsCorrect && mathPowIsCorrect) {
bothCorrect += result;
bothAccurate++;
}
else if (mathLogIsCorrect) {
mathLogWin += result;
calculateUsingMathLogWin++;
}
else if (mathPowIsCorrect) {
mathPowWin += result;
calculateUsingMathPowWin++;
}
else {
bothWrong += result;
bothNotAccurate++;
}
}
}
}
// comment or uncomment as your wish
//System.out.println(bothWrong);
//System.out.println(bothCorrect);
System.out.println(mathLogWin);
System.out.println(mathPowWin);
System.out.println("both wrong : " + bothNotAccurate);
System.out.println("both correct : " + bothAccurate);
System.out.println("Math.log win : " + calculateUsingMathLogWin);
System.out.println("Math.pow win : " + calculateUsingMathPowWin);
}
}
Math.log WIN:
The 3.0 root of 125.0 equals to:
Math.log : 5.0
Math.pow : 4.999999999999999
The 3.0 root of 729.0 equals to:
Math.log : 9.0
Math.pow : 8.999999999999998
The 3.0 root of 1331.0 equals to:
Math.log : 11.0
Math.pow : 10.999999999999998
Math.pow WIN:
The 2.0 root of 4.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 2.0 root of 16.0 equals to:
Math.log : 3.9999999999999996
Math.pow : 4.0
The 2.0 root of 25.0 equals to:
Math.log : 4.999999999999999
Math.pow : 5.0
The 2.0 root of 36.0 equals to:
Math.log : 5.999999999999999
Math.pow : 6.0
The 2.0 root of 49.0 equals to:
Math.log : 6.999999999999999
Math.pow : 7.0
The 2.0 root of 64.0 equals to:
Math.log : 7.999999999999997
Math.pow : 8.0
The 2.0 root of 100.0 equals to:
Math.log : 10.000000000000002
Math.pow : 10.0
The 2.0 root of 144.0 equals to:
Math.log : 11.999999999999998
Math.pow : 12.0
The 2.0 root of 169.0 equals to:
Math.log : 12.999999999999998
Math.pow : 13.0
The 2.0 root of 196.0 equals to:
Math.log : 13.999999999999995
Math.pow : 14.0
The 2.0 root of 225.0 equals to:
Math.log : 14.999999999999998
Math.pow : 15.0
The 2.0 root of 256.0 equals to:
Math.log : 15.999999999999996
Math.pow : 16.0
The 2.0 root of 324.0 equals to:
Math.log : 17.999999999999993
Math.pow : 18.0
The 2.0 root of 361.0 equals to:
Math.log : 18.999999999999993
Math.pow : 19.0
The 2.0 root of 400.0 equals to:
Math.log : 19.999999999999993
Math.pow : 20.0
The 2.0 root of 441.0 equals to:
Math.log : 20.999999999999996
Math.pow : 21.0
The 2.0 root of 529.0 equals to:
Math.log : 22.999999999999996
Math.pow : 23.0
The 2.0 root of 625.0 equals to:
Math.log : 24.99999999999999
Math.pow : 25.0
The 2.0 root of 676.0 equals to:
Math.log : 25.999999999999996
Math.pow : 26.0
The 2.0 root of 729.0 equals to:
Math.log : 26.999999999999996
Math.pow : 27.0
The 2.0 root of 784.0 equals to:
Math.log : 27.999999999999993
Math.pow : 28.0
The 2.0 root of 841.0 equals to:
Math.log : 28.999999999999996
Math.pow : 29.0
The 2.0 root of 900.0 equals to:
Math.log : 29.999999999999996
Math.pow : 30.0
The 2.0 root of 961.0 equals to:
Math.log : 30.999999999999996
Math.pow : 31.0
The 2.0 root of 1024.0 equals to:
Math.log : 31.999999999999993
Math.pow : 32.0
The 2.0 root of 1089.0 equals to:
Math.log : 32.99999999999999
Math.pow : 33.0
The 2.0 root of 1225.0 equals to:
Math.log : 34.999999999999986
Math.pow : 35.0
The 2.0 root of 1296.0 equals to:
Math.log : 35.99999999999999
Math.pow : 36.0
The 2.0 root of 1369.0 equals to:
Math.log : 36.999999999999986
Math.pow : 37.0
The 2.0 root of 1444.0 equals to:
Math.log : 37.999999999999986
Math.pow : 38.0
The 2.0 root of 1521.0 equals to:
Math.log : 38.999999999999986
Math.pow : 39.0
The 2.0 root of 1600.0 equals to:
Math.log : 39.99999999999999
Math.pow : 40.0
The 2.0 root of 1764.0 equals to:
Math.log : 41.99999999999999
Math.pow : 42.0
The 2.0 root of 1849.0 equals to:
Math.log : 42.99999999999999
Math.pow : 43.0
The 2.0 root of 1936.0 equals to:
Math.log : 43.999999999999986
Math.pow : 44.0
The 2.0 root of 2025.0 equals to:
Math.log : 44.999999999999986
Math.pow : 45.0
The 2.0 root of 2116.0 equals to:
Math.log : 45.99999999999999
Math.pow : 46.0
The 2.0 root of 2209.0 equals to:
Math.log : 46.999999999999986
Math.pow : 47.0
The 2.0 root of 2401.0 equals to:
Math.log : 48.999999999999986
Math.pow : 49.0
The 2.0 root of 2500.0 equals to:
Math.log : 49.999999999999986
Math.pow : 50.0
The 2.0 root of 2601.0 equals to:
Math.log : 50.999999999999986
Math.pow : 51.0
The 2.0 root of 2809.0 equals to:
Math.log : 52.99999999999999
Math.pow : 53.0
The 2.0 root of 2916.0 equals to:
Math.log : 53.99999999999999
Math.pow : 54.0
The 2.0 root of 3136.0 equals to:
Math.log : 56.000000000000014
Math.pow : 56.0
The 2.0 root of 3249.0 equals to:
Math.log : 56.99999999999999
Math.pow : 57.0
The 2.0 root of 3364.0 equals to:
Math.log : 57.99999999999997
Math.pow : 58.0
The 2.0 root of 3600.0 equals to:
Math.log : 59.99999999999997
Math.pow : 60.0
The 2.0 root of 3721.0 equals to:
Math.log : 60.99999999999999
Math.pow : 61.0
The 2.0 root of 3844.0 equals to:
Math.log : 61.99999999999999
Math.pow : 62.0
The 2.0 root of 3969.0 equals to:
Math.log : 62.99999999999998
Math.pow : 63.0
The 2.0 root of 4096.0 equals to:
Math.log : 63.999999999999964
Math.pow : 64.0
The 2.0 root of 4225.0 equals to:
Math.log : 64.99999999999997
Math.pow : 65.0
The 2.0 root of 4356.0 equals to:
Math.log : 65.99999999999996
Math.pow : 66.0
The 2.0 root of 4489.0 equals to:
Math.log : 66.99999999999996
Math.pow : 67.0
The 2.0 root of 4900.0 equals to:
Math.log : 70.00000000000001
Math.pow : 70.0
The 2.0 root of 5041.0 equals to:
Math.log : 70.99999999999999
Math.pow : 71.0
The 2.0 root of 5184.0 equals to:
Math.log : 71.99999999999999
Math.pow : 72.0
The 2.0 root of 5329.0 equals to:
Math.log : 72.99999999999997
Math.pow : 73.0
The 2.0 root of 5476.0 equals to:
Math.log : 74.00000000000001
Math.pow : 74.0
The 2.0 root of 5625.0 equals to:
Math.log : 74.99999999999996
Math.pow : 75.0
The 2.0 root of 5776.0 equals to:
Math.log : 75.99999999999999
Math.pow : 76.0
The 2.0 root of 6084.0 equals to:
Math.log : 77.99999999999999
Math.pow : 78.0
The 2.0 root of 6241.0 equals to:
Math.log : 78.99999999999999
Math.pow : 79.0
The 2.0 root of 6400.0 equals to:
Math.log : 79.99999999999994
Math.pow : 80.0
The 2.0 root of 6561.0 equals to:
Math.log : 81.00000000000001
Math.pow : 81.0
The 2.0 root of 6889.0 equals to:
Math.log : 83.00000000000001
Math.pow : 83.0
The 2.0 root of 7056.0 equals to:
Math.log : 83.99999999999996
Math.pow : 84.0
The 2.0 root of 7396.0 equals to:
Math.log : 85.99999999999994
Math.pow : 86.0
The 2.0 root of 7569.0 equals to:
Math.log : 86.99999999999997
Math.pow : 87.0
The 2.0 root of 7744.0 equals to:
Math.log : 88.00000000000001
Math.pow : 88.0
The 2.0 root of 7921.0 equals to:
Math.log : 88.99999999999996
Math.pow : 89.0
The 2.0 root of 8100.0 equals to:
Math.log : 89.99999999999997
Math.pow : 90.0
The 2.0 root of 8281.0 equals to:
Math.log : 90.99999999999996
Math.pow : 91.0
The 2.0 root of 8464.0 equals to:
Math.log : 91.99999999999999
Math.pow : 92.0
The 2.0 root of 8649.0 equals to:
Math.log : 93.00000000000001
Math.pow : 93.0
The 2.0 root of 8836.0 equals to:
Math.log : 93.99999999999997
Math.pow : 94.0
The 2.0 root of 9025.0 equals to:
Math.log : 94.99999999999997
Math.pow : 95.0
The 2.0 root of 9216.0 equals to:
Math.log : 95.99999999999996
Math.pow : 96.0
The 2.0 root of 9409.0 equals to:
Math.log : 96.99999999999997
Math.pow : 97.0
The 2.0 root of 9604.0 equals to:
Math.log : 98.00000000000001
Math.pow : 98.0
The 2.0 root of 9801.0 equals to:
Math.log : 98.99999999999996
Math.pow : 99.0
The 2.0 root of 10000.0 equals to:
Math.log : 100.00000000000001
Math.pow : 100.0
The 3.0 root of 8.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 4.0 root of 16.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 4.0 root of 256.0 equals to:
Math.log : 3.9999999999999996
Math.pow : 4.0
The 4.0 root of 625.0 equals to:
Math.log : 4.999999999999999
Math.pow : 5.0
The 4.0 root of 1296.0 equals to:
Math.log : 5.999999999999999
Math.pow : 6.0
The 4.0 root of 2401.0 equals to:
Math.log : 6.999999999999999
Math.pow : 7.0
The 4.0 root of 4096.0 equals to:
Math.log : 7.999999999999997
Math.pow : 8.0
The 4.0 root of 10000.0 equals to:
Math.log : 10.000000000000002
Math.pow : 10.0
The 5.0 root of 32.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 5.0 root of 243.0 equals to:
Math.log : 2.9999999999999996
Math.pow : 3.0
The 5.0 root of 1024.0 equals to:
Math.log : 3.9999999999999996
Math.pow : 4.0
The 6.0 root of 64.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 7.0 root of 128.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
The 8.0 root of 256.0 equals to:
Math.log : 1.9999999999999998
Math.pow : 2.0
both wrong : 18
both correct : 23
Math.log win : 3
Math.pow win : 96
@imambungo
Copy link
Author

to also include numbers other than whole numbers in the test, delete line 40-42 and 66 of Main.java

@imambungo
Copy link
Author

uncomment line 71 and 72 for a complete result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment