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 )); |
} |
} |