このセクションでは、数値のべき乗を決定する Java プログラムを作成します。数値のべき乗を求めるには、数値に指数を掛けます。
例:
底が 5 で指数が 4 であると仮定します。数値の累乗を求めるには、その数値を 4 回掛けます (5 * 5 * 5 * 5 = 625)。
数値のべき乗を決定するにはどうすればよいですか?
- 基数と指数は読み取るか初期化する必要があります。
- 別の変数べき乗を取得し、それを 1 に設定して結果を保存します。
- for または while ループを使用して、底にべき乗を乗算し、結果をべき乗に格納します。
- 指数がゼロになるまでステップ 3 を繰り返します。
- 出力を印刷します。
数値のべき乗を求める方法
数値のべき乗を決定するにはいくつかの方法があります。
jsの置換
- Java for ループの使用
- Java while ループの使用
- 再帰の使用
- Math.pow() メソッドの使用
- ビット操作の使用
1. Java for ループの使用
for ループを使用すると、基数を繰り返し乗算して数値のべき乗を計算できます。
PowerOfNumber1.java
ピーナッツ vs 落花生
public class PowerOfNumber1 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; for (int i = 0; i <exponent; i++) { result *="base;" } system.out.println(base + ' raised to the power of exponent is result); < pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>2. Using Java while Loop</h3> <p>A while loop may similarly be used to achieve the same result by multiplying the base many times.</p> <p> <strong>PowerOfNumber2.java</strong> </p> <pre> public class PowerOfNumber2 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; int power=3; while (exponent > 0) { result *= base; exponent--; } System.out.println(base + ' raised to the power of ' + power + ' is ' + result); } } </pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>3. Using Recursion:</h3> <p>Recursion is the process of breaking down an issue into smaller sub-problems. Here's an example of how recursion may be used to compute a number's power.</p> <p> <strong>PowerOfNumber3.java</strong> </p> <pre> public class PowerOfNumber3 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = power(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } } </pre> <p> <strong>Output:</strong> </p> <pre> 2 raised to the power of 3 is 8 </pre> <h3>4. Using Math.pow() Method</h3> <p>The java.lang package's Math.pow() function computes the power of an integer directly.</p> <p> <strong>PowerOfNumber4.java</strong> </p> <pre> public class PowerOfNumber4 { public static void main(String[] args) { double base = 2.0; double exponent = 3.0; double result = Math.pow(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 3.0 is 8.0 </pre> <h3>Handling Negative Exponents:</h3> <p>When dealing with negative exponents, the idea of reciprocal powers might be useful. For instance, x^(-n) equals 1/x^n. Here's an example of dealing with negative exponents.</p> <p> <strong>PowerOfNumber5.java</strong> </p> <pre> public class PowerOfNumber5 { public static void main(String[] args) { double base = 2.0; int exponent = -3; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { if (exponent >= 0) { return calculatePositivePower(base, exponent); } else { return 1.0 / calculatePositivePower(base, -exponent); } } static double calculatePositivePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of -3 is: 0.125 </pre> <h3>Optimizing for Integer Exponents:</h3> <p>When dealing with integer exponents, you may optimize the calculation by iterating only as many times as the exponent value. It decreases the number of unneeded multiplications.</p> <p> <strong>PowerOfNumber6.java</strong> </p> <pre> public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;></pre></exponent;></pre></exponent;>
2. Java while ループの使用
while ループも同様に使用して、基数を何度も乗算することで同じ結果を得ることができます。
PowerOfNumber2.java
public class PowerOfNumber2 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = 1; int power=3; while (exponent > 0) { result *= base; exponent--; } System.out.println(base + ' raised to the power of ' + power + ' is ' + result); } }
出力:
2 raised to the power of 3 is 8
3. 再帰の使用:
再帰は、問題をより小さなサブ問題に分解するプロセスです。以下は、再帰を使用して数値のべき乗を計算する方法の例です。
PowerOfNumber3.java
ランタイムエラー
public class PowerOfNumber3 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = power(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } }
出力:
2 raised to the power of 3 is 8
4. Math.pow() メソッドの使用
java.lang パッケージの Math.pow() 関数は、整数のべき乗を直接計算します。
PowerOfNumber4.java
public class PowerOfNumber4 { public static void main(String[] args) { double base = 2.0; double exponent = 3.0; double result = Math.pow(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is ' + result); } }
出力:
2.0 raised to the power of 3.0 is 8.0
負の指数の処理:
負の指数を扱う場合、逆べき乗の考え方が役立つ場合があります。たとえば、x^(-n) は 1/x^n と等しくなります。以下は負の指数を扱う例です。
PowerOfNumber5.java
C++で優先キュー
public class PowerOfNumber5 { public static void main(String[] args) { double base = 2.0; int exponent = -3; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { if (exponent >= 0) { return calculatePositivePower(base, exponent); } else { return 1.0 / calculatePositivePower(base, -exponent); } } static double calculatePositivePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of -3 is: 0.125 </pre> <h3>Optimizing for Integer Exponents:</h3> <p>When dealing with integer exponents, you may optimize the calculation by iterating only as many times as the exponent value. It decreases the number of unneeded multiplications.</p> <p> <strong>PowerOfNumber6.java</strong> </p> <pre> public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;></pre></exponent;>
整数の指数の最適化:
整数の指数を扱う場合、指数の値と同じ回数だけ反復することで計算を最適化できます。不必要な乗算の数が減ります。
PowerOfNumber6.java
public class PowerOfNumber6 { public static void main(String[] args) { double base = 2.0; int exponent = 4; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; for (int i = 0; i <exponent; i++) { result *="base;" } return result; < pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 4 is: 16.0 </pre> <h3>5. Using Bit Manipulation to Calculate Binary Exponents:</h3> <p>Bit manipulation can be used to better improve integer exponents. To do fewer multiplications, an exponent's binary representation might be used.</p> <p> <strong>PowerOfNumber7.java</strong> </p> <pre> public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } } </pre> <p> <strong>Output:</strong> </p> <pre> 2.0 raised to the power of 5 is: 32.0 </pre> <hr></exponent;>
5. ビット操作を使用して 2 進指数を計算する:
ビット操作を使用すると、整数の指数を改善することができます。実行する乗算の数を減らすために、指数のバイナリ表現が使用される場合があります。
PowerOfNumber7.java
public class PowerOfNumber7 { public static void main(String[] args) { double base = 2.0; int exponent = 5; double result = calculatePower(base, exponent); System.out.println(base + ' raised to the power of ' + exponent + ' is: ' + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent > 0) { if ((exponent & 1) == 1) { result *= base; } base *= base; exponent >>= 1; } return result; } }
出力:
2.0 raised to the power of 5 is: 32.0