转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/17528017
前几天有人问我两个整数的最大公约数怎么求,最近工作也不是太忙,就整理了一下关于整数的一些操作和算法,比如:最大公约数、最小公倍数、质数、合数、奇数、偶数、2的整数次幂、完全平方数等。代码中可能了会有点BUG,欢迎在评论中指出。
/**
*@Description: Integer工具类
*/
package cn.lulei.util;
public class IntegerUtil {
/**
* @param a
* @param b
* @return
* @Date: 2013-12-23
* @Author: lulei
* @Description: 求两数的最大公约数
*/
public static int getGreatCommonDivesor(int a, int b){
if (a > b){//保证a是最小的数
int t = a;
a = b;
b = t;
}
//辗转相除
int temp = b % a;
while(temp != 0){
b = a;
a = temp;
temp = b % a;
}
return a;
}
/**
* @param a
* @param b
* @return
* @Date: 2013-12-23
* @Author: lulei
* @Description: 求两个数的最小公倍数,两数相乘除以最大公约数
*/
public static int getLeastCommonMultiple(int a, int b){
return a * b / getGreatCommonDivesor(a, b);
}
/**
* @param n
* @return
* @Date: 2013-12-23
* @Author: lulei
* @Description: 判断一个整数是不是质数
*/
public static boolean isPrimeNumber(int n){
int i;
for (i = 2; i <= n; i++){
if (0 == (n % i))
break;
}
return i == n;
}
/**
* @param n
* @return
* @Date: 2013-12-24
* @Author: lulei
* @Description: 判断一个数是不是合数
*/
public static boolean isCompositeNumber(int n) {
if (n < 2) {
return false;
}
return !isPrimeNumber(n);
}
/**
* @param n
* @return
* @Date: 2013-12-24
* @Author: lulei
* @Description: 判断整数n是不是2的整数次幂
*/
public static boolean is2Exp(int n) {
if (n <= 0) {//如果n为非正数,则直接返回false
return false;
}
return (n & -n) == n;
}
/**
* @param n
* @return
* @Date: 2013-12-24
* @Author: lulei
* @Description: 判断整数是不是奇数
*/
public static boolean isOddNumber(int n) {
return 1 == (n % 2);
}
/**
* @param n
* @return
* @Date: 2013-12-24
* @Author: lulei
* @Description: 判断一个数是不是偶数
*/
public static boolean isEvenNumber(int n) {
return 0 == (n % 2);
}
/**
* @param n
* @return
* @Date: 2013-12-24
* @Author: lulei
* @Description: 判断正数n是不是完全平方数
*/
public static boolean isCompleteSquareNumber(int n){
if (n < 0) {
return false;
}
if (0 == n){
return true;
}
return 0.0 == Math.sqrt(n) - (int)Math.sqrt(n);
}
public static void main(String[] args) {
}
}
今天再添加几个运算:n的非负整数次幂,整数n的位数,判断整数是否是自然幂数等;自然幂数定义参照: http://baike.baidu.com/link?url=tYH9Jy2rcYX-LDb5k6aWtmOAQB4JCR6UsjlZXaCIz_n2y28SX5cN_meGqH8po0Vc#1
类还是上面的 IntegerUtil 类,下面就只添加几个方法
/**
* @param n
* @return
* @Date: 2013-12-25
* @Author: lulei
* @Description: 判断整数是不是回文数
*/
public static boolean isPlalindrome(int n) {
String num = String.valueOf(n);
return new StringBuffer(num).reverse().toString().equalsIgnoreCase(num);
}
/**
* @param n
* @param x x要求必须为非负数,如果x为负,则返回-1
* @return
* @Date: 2013-12-25
* @Author: lulei
* @Description: 求n的x次幂
*/
public static int exp(int n, int x){
if (x < 0) {//如果x为负,则返回-1
return -1;
}
int reInt = 1;
for (int i = 1; i <= x; i++){
reInt *= n;
}
return reInt;
}
/**
* @param n
* @return
* @Date: 2013-12-25
* @Author: lulei
* @Description: 求整数n的位数
*/
public static int getSize(int n) {
n = n < 0 ? -n : n;//取n的绝对值
int size = 1;
while (n >= 10) {
size++;
n /= 10;
}
return size;
}
/**
* @param n 要判断的数字
* @return
* @Date: 2013-12-25
* @Author: lulei
* @Description: 判断一个数是不是自然幂数,即:水仙花数、四叶玫瑰数、五角星数、六合数
* @Description: 北斗七星数、八仙数、九九重阳数、十全十美数
*/
public static boolean isSincePowerNum(int n) {
int x = getSize(n);//求n是几位数
if (x < 3){
return false;
}
int min = exp(10, x - 1);//求x位数的最小值
int max = min * 10;//求x位数的最大值
if (n < min || n > max){
return false;//如果n不是x位数,则直接返回false
}
ArrayList<Integer> num = new ArrayList<Integer>();//存储整数n除个位以外的数
int nCopy = n;
while (nCopy > 10) {
num.add(nCopy / min);
nCopy %= min;
min /= 10;
}
int sum = exp(nCopy, x);//将个位数的x幂赋值给sum
for (int i : num){
sum += exp(i, x);
}
return sum == n;
}