Java工具類---數字計算工具 NumberUtil

jopen 11年前發布 | 44K 次閱讀 Java工具類 Java開發

package com.luang.util.common;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

/**

  • NumberUtil.java *
  • @desc 數字計算工具
  • @author Guoxp
  • @datatime Apr 7, 2013 3:52:29 PM / public class NumberUtil {

    /** 生成不重復隨機數

    • 根據給定的最小數字和最大數字,以及隨機數的個數,產生指定的不重復的數組
    • @param begin 最小數字(包含該數)
    • @param end 最大數字(不包含該數)
    • @param size 指定產生隨機數的個數
      */
      public int[] generateRandomNumber(int begin, int end, int size) {
      // 加入邏輯判斷,確保begin<end并且size不能大于該表示范圍
      if (begin >= end || (end - begin) < size) {

       return null;    
      

      }
      // 種子你可以隨意生成,但不能重復
      int[] seed = new int[end - begin];

      for (int i = begin; i < end; i ++) {

       seed[i - begin] = i;    
      

      }
      int[] ranArr = new int[size];
      Random ran = new Random();
      // 數量你可以自己定義。
      for (int i = 0; i < size; i++) {

       // 得到一個位置    
       int j = ran.nextInt(seed.length - i);               
       // 得到那個位置的數值    
       ranArr[i] = seed[j];    
       // 將最后一個未用的數字放到這里    
       seed[j] = seed[seed.length - 1 - i];    
      

      }
      return ranArr;
      }

/**  生成不重復隨機數
 * 根據給定的最小數字和最大數字,以及隨機數的個數,產生指定的不重復的數組  
 * @param begin 最小數字(包含該數)  
 * @param end 最大數字(不包含該數)  
 * @param size 指定產生隨機數的個數  
 */    
 public Integer[] generateBySet(int begin, int end, int size) {    
    // 加入邏輯判斷,確保begin<end并且size不能大于該表示范圍    
    if (begin >= end || (end - begin) < size) {    
        return null;    
    }    

    Random ran = new Random();    
    Set<Integer> set = new HashSet<Integer>();    
    while (set.size() < size) {    
        set.add(begin + ran.nextInt(end - begin));    
    }    

    Integer[] ranArr = new Integer[size];    
    ranArr = set.toArray(new Integer[size]);    
    //ranArr = (Integer[]) set.toArray();    

    return ranArr;    
}  
/** 
* 判斷String是否是整數 
*/  
public boolean isInteger(String s){  
    if((s != null)&&(s!=""))  
     return s.matches("^[0-9]*$");  
    else  
     return false;  
}  
/** 
* 判斷字符串是否是浮點數 
*/  
public boolean isDouble(String value) {  
    try {  
       Double.parseDouble(value);  
       if (value.contains("."))  
           return true;  
       return false;  
    } catch (NumberFormatException e) {  
       return false;  
    }  
}  
/** 
* 判斷字符串是否是數字 
*/  
public boolean isNumber(String value) {  
    return isInteger(value) || isDouble(value);  
}  

    //排序方法  
public static void sort(int[] array) {// 小到大的排序  
    int temp = 0;  
    for (int i = 0; i < array.length; i++) {  
        for (int j = i; j < array.length; j++) {  
            if (array[i] > array[j]) {  
                temp = array[i];  
                array[i] = array[j];  
                array[j] = temp;  
            }  
        }  
    }  
}  

/** 
 * 是否是質數 
 */  
public static boolean isPrimes(int n) {  
    for (int i = 2; i <= Math.sqrt(n); i++) {  
        if (n % i == 0) {  
            return false;  
        }  
    }  
    return true;  
}  

/** 
 * 階乘 
 * @param n 
 * @return 
 */  
public static int factorial(int n) {  
    if (n == 1) {  
        return 1;  
    }  
    return n * factorial(n - 1);  
}  
/** 
 * 平方根算法 
 * @param x 
 * @return 
 */  
public static long sqrt(long x) {  
    long y = 0;  
    long b = (~Long.MAX_VALUE) >>> 1;  
    while (b > 0) {  
        if (x >= y + b) {  
             x -= y + b;  
             y >>= 1;  
             y += b;  
        } else {  
             y >>= 1;  
        }   
        b >>= 2;  
   }   
    return y;  
}  

private int math_subnode(int selectNum, int minNum) {  
    if (selectNum == minNum) {  
        return 1;  
    } else {  
        return selectNum * math_subnode(selectNum - 1, minNum);  
    }  
}  

private int math_node(int selectNum) {  
    if (selectNum == 0) {  
        return 1;  
    } else {  
        return selectNum * math_node(selectNum - 1);  
    }  
}  
/** 
 * 可以用于計算雙色球、大樂透注數的方法 
 * selectNum:選中了的小球個數 
 * minNum:至少要選中多少個小球 
 * 比如大樂透35選5可以這樣調用processMultiple(7,5); 
 * 就是數學中的:C75=7*6/2*1 
 */  
public int processMultiple(int selectNum, int minNum) {  
    int result;  
    result = math_subnode(selectNum, minNum)  
            / math_node(selectNum - minNum);  
    return result;  
}  

/**

  • 求m和n的最大公約數 */
    public static int gongyue(int m, int n) {

      while (m % n != 0) {     
          int temp = m % n;     
          m = n;     
          n = temp;     
      }     
      return n;     
    

    }

    /**

    • 求兩數的最小公倍數
      /
      public static int gongbei(int m, int n) {
      return m
      n / gongyue(m, n);
      }

      /**

    • 遞歸求兩數的最大公約數
      */
      public static int divisor(int m,int n){
      if(m%n==0){
      return n;     
      
      }else{
      return divisor(n,m%n);     
      
      }
      }
      public static void main(String[] args){
      NumberUtil util=new NumberUtil();
      System.out.println(util.sqrt(100)); }
      } </pre>來自:http://blog.csdn.net/guoxuepeng123/article/details/8797928
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!