package c;
import java.math.BigInteger;
public class HighPow {
public static void main(String[] args) {
long begin1=System.currentTimeMillis();
System.out.println(pow(new BigInteger("999999") ,99));
long end1 = System.currentTimeMillis();
System.out.println("快效率"+(end1-begin1));
long begin2=System.currentTimeMillis();
System.out.println("begin2=="+begin2);
System.out.println(pow1(new BigInteger("999999") ,99));
long end2 = System.currentTimeMillis();
System.out.println("end2=="+end2);
System.out.println("慢效率"+(end2-begin2));
}
private static BigInteger pow1(BigInteger x, int n) {
BigInteger sum=BigInteger.valueOf(1);
for (int i = 0; i < n; i++) {
sum=x.multiply(sum);
}
return sum;
}
private static BigInteger pow(BigInteger x, int n) {
if(n==0){
return BigInteger.valueOf(1);
}
if(n==1){
return x;
}
if(n%2==0){ //判断n奇偶
return pow(x.multiply(x),n/2);
}
else return pow(x.multiply(x),n/2).multiply(x);
}
}
999901004850843154764304477976514369139655131740633508461194231534839921730061928165918468524559784711035127337458000315861841709428648449965581720563036528559778975449288816486108390262378384273170489214859673210348574933590756152010150151328940519978842541428941575809198981648563222986501427953560053044625798093604122762079193632166451298745515082518566720858255910313691329603648838009949658744782958530043017504914393433729464748384929764551655116938062519252825189492597754483209164432338340725827672754454905076614553527220234774443152131030423470815523140235624156848995149000098999999
快效率29
begin2==1431856260255
999901004850843154764304477976514369139655131740633508461194231534839921730061928165918468524559784711035127337458000315861841709428648449965581720563036528559778975449288816486108390262378384273170489214859673210348574933590756152010150151328940519978842541428941575809198981648563222986501427953560053044625798093604122762079193632166451298745515082518566720858255910313691329603648838009949658744782958530043017504914393433729464748384929764551655116938062519252825189492597754483209164432338340725827672754454905076614553527220234774443152131030423470815523140235624156848995149000098999999
end2==1431856260265
慢效率10
为啥我把快效率的放在慢效率前面打印,它的时间就长了啊 递归的效率log(N)不是快于O(n)嘛????
999901004850843154764304477976514369139655131740633508461194231534839921730061928165918468524559784711035127337458000315861841709428648449965581720563036528559778975449288816486108390262378384273170489214859673210348574933590756152010150151328940519978842541428941575809198981648563222986501427953560053044625798093604122762079193632166451298745515082518566720858255910313691329603648838009949658744782958530043017504914393433729464748384929764551655116938062519252825189492597754483209164432338340725827672754454905076614553527220234774443152131030423470815523140235624156848995149000098999999
快效率29
begin2==1431856260255
999901004850843154764304477976514369139655131740633508461194231534839921730061928165918468524559784711035127337458000315861841709428648449965581720563036528559778975449288816486108390262378384273170489214859673210348574933590756152010150151328940519978842541428941575809198981648563222986501427953560053044625798093604122762079193632166451298745515082518566720858255910313691329603648838009949658744782958530043017504914393433729464748384929764551655116938062519252825189492597754483209164432338340725827672754454905076614553527220234774443152131030423470815523140235624156848995149000098999999
end2==1431856260265
慢效率10
为啥我把快效率的放在慢效率前面打印,它的时间就长了啊 递归的效率log(N)不是快于O(n)嘛????
输出控制台也是要时间的
你把所有的算完了之后在一起输出
logN>N的前提是N足够大。
输出控制台也是要时间的
你把所有的算完了之后在一起输出
输出控制台也是要时间的
你把所有的算完了之后在一起输出