logo

Kraften til et tall i Java

I denne delen skal vi skrive Java-programmer for å bestemme kraften til et tall. For å få potensen til et tall, multipliser tallet med eksponenten.

Eksempel:

Anta at grunntallet er 5 og eksponenten er 4. For å få potensen til et tall, multipliser det med seg selv fire ganger, dvs. (5 * 5 * 5 * 5 = 625).

Hvordan bestemme kraften til et tall?

  • Base og eksponent bør leses eller initialiseres.
  • Ta en annen variabel kraft og sett den til 1 for å lagre utfallet.
  • Multipliser basen med makt og lagre resultatet i kraft ved å bruke for- eller while-løkken.
  • Gjenta trinn 3 til eksponenten er lik null.
  • Skriv ut utskriften.

Metoder for å finne kraften til et tall

Det er flere metoder for å bestemme et talls potens:

btree og b tree
  1. Bruker Java for Loop
  2. Bruker Java mens Loop
  3. Bruker rekursjon
  4. Bruke Math.pow()-metoden
  5. Bruke bitmanipulasjon

1. Bruke Java for Loop

En for-løkke kan brukes til å beregne potensen til et tall ved å multiplisere grunntallet med seg selv gjentatte ganger.

PowerOfNumber1.java

string sammenligne java
 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 &gt; 0) { result *= base; exponent--; } System.out.println(base + &apos; raised to the power of &apos; + power + &apos; is &apos; + 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&apos;s an example of how recursion may be used to compute a number&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is &apos; + 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&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is &apos; + 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&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { if (exponent &gt;= 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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + 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&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent &gt; 0) { if ((exponent &amp; 1) == 1) { result *= base; } base *= base; exponent &gt;&gt;= 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. Bruke Java mens Loop

En while-løkke kan på samme måte brukes for å oppnå samme resultat ved å multiplisere basen mange ganger.

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 &gt; 0) { result *= base; exponent--; } System.out.println(base + &apos; raised to the power of &apos; + power + &apos; is &apos; + result); } } 

Produksjon:

 2 raised to the power of 3 is 8 

3. Bruke rekursjon:

Rekursjon er prosessen med å bryte ned et problem i mindre delproblemer. Her er et eksempel på hvordan rekursjon kan brukes til å beregne et talls potens.

PowerOfNumber3.java

det vakreste smilet i verden
 public class PowerOfNumber3 { public static void main(String[] args) { int base = 2; int exponent = 3; int result = power(base, exponent); System.out.println(base + &apos; raised to the power of &apos; + exponent + &apos; is &apos; + result); } public static int power(int base, int exponent) { if (exponent == 0) { return 1; } else { return base * power(base, exponent - 1); } } } 

Produksjon:

 2 raised to the power of 3 is 8 

4. Bruke Math.pow()-metoden

Java.lang-pakkens Math.pow()-funksjon beregner kraften til et heltall direkte.

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 + &apos; raised to the power of &apos; + exponent + &apos; is &apos; + result); } } 

Produksjon:

 2.0 raised to the power of 3.0 is 8.0 

Håndtering av negative eksponenter:

Når du arbeider med negative eksponenter, kan ideen om gjensidige krefter være nyttig. For eksempel er x^(-n) lik 1/x^n. Her er et eksempel på hvordan man håndterer negative eksponenter.

PowerOfNumber5.java

konverter streng til enum
 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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { if (exponent &gt;= 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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + 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&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent &gt; 0) { if ((exponent &amp; 1) == 1) { result *= base; } base *= base; exponent &gt;&gt;= 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;>

Optimalisering for heltallseksponenter:

Når du arbeider med heltallseksponenter, kan du optimalisere beregningen ved å iterere så mange ganger som eksponentverdien. Det reduserer antallet unødvendige multiplikasjoner.

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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + 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&apos;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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent &gt; 0) { if ((exponent &amp; 1) == 1) { result *= base; } base *= base; exponent &gt;&gt;= 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. Bruke bitmanipulasjon for å beregne binære eksponenter:

Bitmanipulasjon kan brukes til å forbedre heltallseksponenter bedre. For å gjøre færre multiplikasjoner, kan en eksponents binære representasjon brukes.

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 + &apos; raised to the power of &apos; + exponent + &apos; is: &apos; + result); } static double calculatePower(double base, int exponent) { double result = 1.0; while (exponent &gt; 0) { if ((exponent &amp; 1) == 1) { result *= base; } base *= base; exponent &gt;&gt;= 1; } return result; } } 

Produksjon:

 2.0 raised to the power of 5 is: 32.0