如何判断一个数时幂数?
2006-08-24 17:46
120 查看
这个来自于csdn的一篇文章,我总结了下,就写了这个java程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class PowerExponent ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentGreatestCommonDivisor = 0;//记录计算时当前质因子个数 之间的最大的公约数初始化为0表示不可用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentPrimerDivisorCount =0 ; //用于记录当前质因素 个数计数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int prePrimerDivisorCount =0;//前一个质因数计数个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentPrimerDivisor =0; //先前质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void setCurrentGreatestCommonDivisor(int value)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.currentGreatestCommonDivisor = value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int getCurrentGreatestCommonDivisor()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.currentGreatestCommonDivisor ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public boolean isPowerExponentNumber(final int orignNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int number =orignNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
prePrimerDivisorCount =0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor =0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = 0;//初始化为0表示不可用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisorCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number>3)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number%2 == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
number /= 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
++currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number >=i )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number%i == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i == currentPrimerDivisor)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
++currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//新的质因素开始,计数个数设置为1 ,统计前一个质数的个数,记录当前质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
prePrimerDivisorCount = currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisorCount = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果最大公约数!=0表明一定不是第一次求最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = PowerExponent.FindGreatestCommonDivisor(currentGreatestCommonDivisor,prePrimerDivisorCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = prePrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
number /=i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i*i >orignNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//存在一个比它开方还大的质因子所以必非幂数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i+=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//质因素分解结束 number == 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 0 && currentPrimerDivisorCount >1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//只有一个质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//求所有质因子个数的 最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = PowerExponent.FindGreatestCommonDivisor(currentGreatestCommonDivisor,currentPrimerDivisorCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param a a>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param b b>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 最大质因素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int FindGreatestCommonDivisor(final int orignNumber_a,final int orignNumber_b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int a= orignNumber_a;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int b= orignNumber_b;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a==1 || b== 1) return 1;//互质
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( a%b == 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return b;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(b%a == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return a;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int product =1 ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int greatesCommonDivisor = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a%2 == 0&& b%2 ==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a /= 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b /=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
product *=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a>=i&&b>=i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a%i == 0 && b%i ==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a /= i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b /= i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
product *=i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i * i > orignNumber_a || i*i > orignNumber_b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当前的a ,b存在一个比它开方还大的质因子所以没有必要再分解质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
greatesCommonDivisor = product;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return greatesCommonDivisor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i += 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
greatesCommonDivisor = product;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return greatesCommonDivisor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PowerExponent pe = new PowerExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean bRet = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long startMills = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<64000;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(i+"是幂指数吗?"+bRet);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(bRet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i+"是幂指数");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(2147395600);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(bRet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(2147395600+"是幂指数");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(2*2*2*2*3*3*3*3*3*3*5*5*5*5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(2*2*2*2*3*3*3*3*3*3*5*5*5*5+"=2*2*2*2*3*3*3*3*3*3*5*5*5*5*7*7"+"是幂指数吗"+bRet);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long endMills = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("耗时"+(endMills - startMills));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(PowerExponent.FindGreatestCommonDivisor(2*2*2*3*3*3*7*7*11*13,2*2*2*3*3*5*7*13));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我的思路也在那篇文章中写过就是
所有质因数的个数构成的集合A{a1,a2,。。。}
而集合A中元素a1,a2,a3最大公约数s
如果s>1,那么N就是幂数。
比如果
2*2*2*2*3*3*3*3*3*3*3 质因数个数的集合{4,6}最大公约数为2〉1所以是幂数
5*5*5 质因数个数的集合{3}最大公约数为3>1所以是幂数
因为质因数个数的最大公约数为s
那么队以任意质因数 pi,它出现的次数为ai;
必有ai %s = 0,所以一定能够将该质因数pi 分成s组,每组ai/s个。他的积为pi^(ai/s)
这样就将将所有质因数都分成了s组
原数N= {pi^(ai/s)} ^s,所以幂指数为s,底数为 pi^(ai/s)之积 注 i=1,2,3,..
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
public class PowerExponent ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentGreatestCommonDivisor = 0;//记录计算时当前质因子个数 之间的最大的公约数初始化为0表示不可用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentPrimerDivisorCount =0 ; //用于记录当前质因素 个数计数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int prePrimerDivisorCount =0;//前一个质因数计数个数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int currentPrimerDivisor =0; //先前质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void setCurrentGreatestCommonDivisor(int value)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.currentGreatestCommonDivisor = value;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int getCurrentGreatestCommonDivisor()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return this.currentGreatestCommonDivisor ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public boolean isPowerExponentNumber(final int orignNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int number =orignNumber;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
prePrimerDivisorCount =0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor =0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = 0;//初始化为0表示不可用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisorCount = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(number>3)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor = 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number%2 == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
number /= 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
++currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number >=i )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(number%i == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i == currentPrimerDivisor)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
++currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//新的质因素开始,计数个数设置为1 ,统计前一个质数的个数,记录当前质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
prePrimerDivisorCount = currentPrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisor = i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentPrimerDivisorCount = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//判断最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor != 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//如果最大公约数!=0表明一定不是第一次求最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = PowerExponent.FindGreatestCommonDivisor(currentGreatestCommonDivisor,prePrimerDivisorCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = prePrimerDivisorCount;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
number /=i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i*i >orignNumber)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//存在一个比它开方还大的质因子所以必非幂数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i+=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//质因素分解结束 number == 1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 0 && currentPrimerDivisorCount >1 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//只有一个质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//求所有质因子个数的 最大公约数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
currentGreatestCommonDivisor = PowerExponent.FindGreatestCommonDivisor(currentGreatestCommonDivisor,currentPrimerDivisorCount);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(currentGreatestCommonDivisor == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else return true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
/** *//**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param a a>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param b b>0
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 最大质因素
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public static int FindGreatestCommonDivisor(final int orignNumber_a,final int orignNumber_b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int a= orignNumber_a;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int b= orignNumber_b;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(a==1 || b== 1) return 1;//互质
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if( a%b == 0 )
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return b;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(b%a == 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return a;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int product =1 ;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int greatesCommonDivisor = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a%2 == 0&& b%2 ==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a /= 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b /=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
product *=2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
int i = 3;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a>=i&&b>=i)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
while(a%i == 0 && b%i ==0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
a /= i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
b /= i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
product *=i;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(i * i > orignNumber_a || i*i > orignNumber_b)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//当前的a ,b存在一个比它开方还大的质因子所以没有必要再分解质因子
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
greatesCommonDivisor = product;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return greatesCommonDivisor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
i += 2;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
greatesCommonDivisor = product;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return greatesCommonDivisor;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
public static void main(String[] args) ...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// TODO Auto-generated method stub
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
PowerExponent pe = new PowerExponent();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean bRet = false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long startMills = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for(int i=1;i<64000;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(i);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(i+"是幂指数吗?"+bRet);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(bRet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(i+"是幂指数");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(2147395600);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(bRet)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(2147395600+"是幂指数");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
bRet = pe.isPowerExponentNumber(2*2*2*2*3*3*3*3*3*3*5*5*5*5);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(2*2*2*2*3*3*3*3*3*3*5*5*5*5+"=2*2*2*2*3*3*3*3*3*3*5*5*5*5*7*7"+"是幂指数吗"+bRet);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
long endMills = System.currentTimeMillis();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("耗时"+(endMills - startMills));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//System.out.println(PowerExponent.FindGreatestCommonDivisor(2*2*2*3*3*3*7*7*11*13,2*2*2*3*3*5*7*13));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
我的思路也在那篇文章中写过就是
所有质因数的个数构成的集合A{a1,a2,。。。}
而集合A中元素a1,a2,a3最大公约数s
如果s>1,那么N就是幂数。
比如果
2*2*2*2*3*3*3*3*3*3*3 质因数个数的集合{4,6}最大公约数为2〉1所以是幂数
5*5*5 质因数个数的集合{3}最大公约数为3>1所以是幂数
因为质因数个数的最大公约数为s
那么队以任意质因数 pi,它出现的次数为ai;
必有ai %s = 0,所以一定能够将该质因数pi 分成s组,每组ai/s个。他的积为pi^(ai/s)
这样就将将所有质因数都分成了s组
原数N= {pi^(ai/s)} ^s,所以幂指数为s,底数为 pi^(ai/s)之积 注 i=1,2,3,..
相关文章推荐
- 如何判断一个点是否在不规则图形内部?
- 微软面试(or电面)试题——如何判断一个单链表是否存在回路
- Android 如何判断一个应用在运行
- 已知子网掩码如何判断两个IP地址是不是在同一个网段
- iOS 如何判断一个点在圆、方框、三角形区域内?
- ios 动态添加了按钮,绑定同一个点击事件,如何判断点击的是哪个?
- 如何判断一个数组是否按顺序排好了
- jquery和js如何判断一个对象是否存在
- Matlab如何判断一个点是否落在一个不规则的封闭区域中
- 如何判断一个操作系统是16位还是32位的?不能用sizeof()函数
- 如何判断一个数是否是2的幂次方数
- ios 动态添加了按钮,绑定同一个点击事件,如何判断点击的是哪个?
- 在shell中如何判断一个变量是否为空
- 如何判断一个三角型是逆时针还是顺时针
- 如何检测或判断一个文件或字节流(无BOM)是什么编码类型
- 如何判断一个(压缩)文件的类型,以及zcat/gzcat
- 如何判断一个对象是否是数组。
- 如何判断一个GPS点是否在以另一个GPS点为圆心100米为半径的圆内(Java代码)
- 如何判断一个进程已经完成初始化?
- jquery和js如何判断一个对象是否存在