数学运算(素数质数整数浮点数判断/阶乘/byte数组转换为16进制/打印九九乘法表)

package 运算及类型转换类;

import java.util.Date;
import java.util.regex.Pattern;

public class 数学运算 {
	/**
	 * 判断任意一个整数是否素数 isPrimes
	 * 
	 * @param n
	 * @return boolean
	 */
	public static boolean 判断任意一个整数是否素数(int n) {
		for (int i = 2; i <= Math.sqrt(n); i++) {
			if (n % i == 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 获得任意一个整数的阶乘,递归 factorial
	 * 
	 * @param n
	 * @return n!
	 */
	public static int 获得任意一个整数的阶乘(int n) {
		if (n == 1) {
			return 1;
		}
		return n * 获得任意一个整数的阶乘(n - 1);
	}

	/**
	 * 将指定byte数组以16进制的形式打印到控制台 printHexString
	 * 
	 * @param hint
	 *            String
	 * @param b
	 *            byte[]
	 * @return void
	 */
	public static void 将指定byte数组以16进制的形式打印到控制台(byte[] b) {

		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			System.out.print(hex.toUpperCase() + " ");
		}
		System.out.println("");
	}

	/**
	 * 判断是否为质数 isPrime
	 * 
	 * @param x
	 * @return
	 */
	public static boolean 判断是否为质数(int x) {
		if (x <= 7) {
			if (x == 2 || x == 3 || x == 5 || x == 7)
				return true;
		}
		int c = 7;
		if (x % 2 == 0)
			return false;
		if (x % 3 == 0)
			return false;
		if (x % 5 == 0)
			return false;
		int end = (int) Math.sqrt(x);
		while (c <= end) {
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 4;
			if (x % c == 0) {
				return false;
			}
			c += 6;
			if (x % c == 0) {
				return false;
			}
			c += 2;
			if (x % c == 0) {
				return false;
			}
			c += 6;
		}
		return true;
	}

	/**
	 * 判断是否为整数 isInteger
	 * 
	 * @param str 传入的字符串
	 * @return    是整数返回true,否则返回false
	 */
	public static boolean 判断是否为整数(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 * 判断是否为浮点数,包括double和float isDouble
	 * 
	 * @param str
	 *            传入的字符串
	 * @return 是浮点数返回true,否则返回false
	 */
	public static boolean 判断是否为浮点数(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");
		return pattern.matcher(str).matches();
	}

	
	    /** 
	    *一个for循环打印九九乘法表 
	    *nineNineMultiTable
	    */ 
	    public void 打印九九乘法表() 
	    { 
	      for (int i = 1,j = 1; j <= 9; i++) { 
	          System.out.print(i+"*"+j+"="+i*j+" "); 
	          if(i==j) 
	          { 
	              i=0; 
	              j++; 
	              System.out.println(); 
	          } 
	      } 
	    } 

	
	public static void main(String[] args) {
		System.out.println(获得任意一个整数的阶乘(4));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值