|
@@ -3,77 +3,47 @@ import java.util.Arrays;
|
|
|
import flanagan.io.Db;
|
|
|
import java.math.BigInteger;
|
|
|
|
|
|
-/**
|
|
|
- * Das Program liest eine ganze Zahl N (0 <= N <= 20) und
|
|
|
- * berechnet den Wert von N! = 1 * 2 * ... * (N - 1) * N, wobei
|
|
|
- * 0! = 1 (nach Definition)
|
|
|
- */
|
|
|
-public class bigIntegerPotenzen
|
|
|
-{
|
|
|
- public static void main(String [] args)
|
|
|
- {
|
|
|
- int N = 0;
|
|
|
- int B = 3;
|
|
|
+public class bigIntegerPotenzen {
|
|
|
+ public static void main(String [] args) {
|
|
|
|
|
|
- /* Bedeutung:
|
|
|
- * N = die gesuchte Fakultaet
|
|
|
- * n = laufende Fakultaet-Zahl
|
|
|
- * resn = leufende Fakultaet: resn = (n - 1)!, n = 0, 1, 2, ... N
|
|
|
- * res = Resultat: res = n! = n*(n - 1)!, n = 0, 1, 2, ... N
|
|
|
- * = n* resn
|
|
|
- */
|
|
|
+ // init variables
|
|
|
+ int exponent = 0;
|
|
|
+ int base = 3;
|
|
|
+ BigInteger res;
|
|
|
|
|
|
- // get Basiszahl input
|
|
|
+ // get base input
|
|
|
try {
|
|
|
- B = Db.readInt("Basiszahl:" +
|
|
|
- "bestätige mit ENTER ");
|
|
|
+ base = Db.readInt("base = ");
|
|
|
} catch (NumberFormatException e) {
|
|
|
+ Db.show("Error: no valid integer input provided");
|
|
|
System.exit(1);
|
|
|
}
|
|
|
|
|
|
- // get Exponent input
|
|
|
+ // get exponent input
|
|
|
try {
|
|
|
- N = Db.readInt("Exponent:" +
|
|
|
- "bestätige mit ENTER ");
|
|
|
+ exponent = Db.readInt("exponent = ");
|
|
|
} catch (NumberFormatException e) {
|
|
|
+ Db.show("Error: no valid integer input provided");
|
|
|
System.exit(1);
|
|
|
}
|
|
|
|
|
|
- // validate that: N, B >= 0
|
|
|
-
|
|
|
- if ( B < 0) {
|
|
|
- Db.show(
|
|
|
- "Falsche Eingabe: der Input-Parameter N < 0 \n"+
|
|
|
- "Probieren Sie nochmals! Das Programm ist sofort beendet!");
|
|
|
+ // validate that: exponent, base >= 0
|
|
|
+ if (base < 0) {
|
|
|
+ Db.show("Error: 'base' cannot be negative");
|
|
|
+ System.exit(1);
|
|
|
+ } else if (exponent < 0) {
|
|
|
+ Db.show("Error: 'exponent' cannot be negative");
|
|
|
System.exit(1);
|
|
|
}
|
|
|
|
|
|
// use big integer
|
|
|
- BigInteger bigN = BigInteger.valueOf(N);
|
|
|
- BigInteger bigB = BigInteger.valueOf(B);
|
|
|
- bigN.add(BigInteger.ONE);
|
|
|
- bigN = bigN.add(BigInteger.ONE);
|
|
|
- BigInteger res = BigInteger.valueOf(1);
|
|
|
- BigInteger resn = BigInteger.valueOf(1);
|
|
|
-
|
|
|
- //for (BigInteger n = BigInteger.valueOf(1); n.compareTo(bigN) < 0; n = n.add(BigInteger.ONE))
|
|
|
- if (N < 0) {
|
|
|
- //res = BigInteger.valueOf(1337);
|
|
|
- res = BigInteger.ONE.divide(BigInteger.valueOf(2));
|
|
|
- double negativeResult = res.doubleValue();
|
|
|
- JOptionPane.showMessageDialog (null,
|
|
|
- B + "^" + N + " = " + negativeResult,
|
|
|
- "Lösung", JOptionPane.INFORMATION_MESSAGE);
|
|
|
- } else {
|
|
|
- res = bigB.pow(N);
|
|
|
- resn = res;
|
|
|
+ BigInteger bigN = BigInteger.valueOf(exponent);
|
|
|
+ BigInteger bigB = BigInteger.valueOf(base);
|
|
|
|
|
|
- JOptionPane.showMessageDialog (null,
|
|
|
- B + "^" + N + " = " + res,
|
|
|
- "Lösung", JOptionPane.INFORMATION_MESSAGE);
|
|
|
- }
|
|
|
-
|
|
|
- // solution prompt
|
|
|
+ for (int n = 0; n <= exponent; n++) {
|
|
|
+ res = bigB.pow(n);
|
|
|
|
|
|
+ System.out.format("n = %d \t %d^%d \t %d (%d-digit)\n", n, base, n, res, res.toString().length());
|
|
|
+ }
|
|
|
}
|
|
|
}
|