# 以Java计算能力

Python中有没有像Python那样简单的东西？

### 10 Solutions collect form web for “以Java计算能力”

` `long pow (long a, int b) { if ( b == 0) return 1; if ( b == 1) return a; if (isEven( b )) return pow ( a * a, b/2); //even a=(a^2)^b/2 else return a * pow ( a * a, b/2); //odd a=a*(a^2)^b/2 }` `

` `long result = 1; for (int i = 1; i <= b; i++) { result *= a; }` `

` `int result = (int)Math.pow(a, b);` `

`pow(int b, int k)`计算b到k的权力，并包装溢出

`checkedPow(int b, int k)`除了在溢出时抛出`ArithmeticException`之外是相同的

` `int x = (int) Math.pow(a,b);` `

` `import java.util.*; public class Power { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int num = 0; int pow = 0; int power = 0; System.out.print("Enter number: "); num = sc.nextInt(); System.out.print("Enter power: "); pow = sc.nextInt(); System.out.print(power(num,pow)); } public static int power(int a, int b) { int power = 1; for(int c=0;c<b;c++) power*=a; return power; } }` `

` `/** Compute a**p, assume result fits in a 32-bit signed integer */ int pow(int a, int p) { int res = 1; int i1 = 31 - Integer.numberOfLeadingZeros(p); // highest bit index for (int i = i1; i >= 0; --i) { res *= res; if ((p & (1<<i)) > 0) res *= a; } return res; }` `

` `private static int pow(int x, int n) { if (n == 0) return 1; if (n == 1) return x; if (n < 0) { // always 1^xx = 1 && 2^-1 (=0.5 --> ~ 1 ) if (x == 1 || (x == 2 && n == -1)) return 1; else return 0; } if ((n & 1) == 0) { //is even long num = pow(x * x, n / 2); if (num > Integer.MAX_VALUE) //check bounds return Integer.MAX_VALUE; return (int) num; } else { long num = x * pow(x * x, n / 2); if (num > Integer.MAX_VALUE) //check bounds return Integer.MAX_VALUE; return (int) num; } }` `

` `double pow(double base, double exponent)` `

` `import java.util.*; import java.lang.*; // CONTAINS THE Math library public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n= sc.nextInt(); // Accept integer n int m = sc.nextInt(); // Accept integer m int ans = (int) Math.pow(n,m); // Calculates n ^ m System.out.println(ans); // prints answers } }` `

` `import java.math.*; public class BigIntegerDemo { public static void main(String[] args) { BigInteger bi1, bi2; // create 2 BigInteger objects int exponent = 2; // create and assign value to exponent // assign value to bi1 bi1 = new BigInteger("6"); // perform pow operation on bi1 using exponent bi2 = bi1.pow(exponent); String str = "Result is " + bi1 + "^" +exponent+ " = " +bi2; // print bi2 value System.out.println( str ); } }` `
• 三维相对angular度和计算
• Javascript：将四舍五入的数字格式化为N小数
• 如何强制JS做math而不是把两个string放在一起
• 我怎样才能得到罪，cos和tan来使用度数而不是弧度？
• 我如何用从零开始的可读angular度来组合旋转matrix？
• 如何在一组数字上findGCD，LCM
• PHP中的组合，部署和排列
• Photoshop如何将两个图像混合在一起？
• 用javascript浮点数总和
• 正数在JavaScript中的负数？
• 评估string“3 *（4 + 2）”收益int 18