如果我的代码有任何问题,有人可以发现吗?我正在尝试计算执行此阶乘所需的时间并将其转换为秒,但它似乎打印出错误的值。谢谢!!
long startTime = System.nanoTime();
BigInteger factorial_minute = doFactorial(50000);
long endTime = System.nanoTime();
String factorial_minute_str = factorial_minute.toString();
System.out.println("Largest n! in one minute = " + factorial_minute);
System.out.println("n! in one minute Amount of Digits: " + factorial_minute_str.length()); // Print the length also
System.out.println("Total execution time: " + (int)((endTime - startTime)/1000000000 + " seconds") );
输出是
Total execution time: 4 seconds
编辑,我把它改成了1000000000,但是输出好像还是错误。
最佳答案
一纳秒是十亿分之一秒,而不是百万分之一秒。在除数上再加三个零。
(endTime - startTime)/1000000000
关于java - 时间没有将纳秒转换为秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14617206/