Эх сурвалжийг харах

reasonably finished bigInteger programs

Noah Vogt 4 жил өмнө
parent
commit
0d88f4985f

+ 20 - 54
inputOutputFlanagan/bigIntegerFakultaet.java

@@ -3,79 +3,45 @@ 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 bigIntegerFakultaet
-{
-    public static void main(String [] args)
-    {
-        int N = 0;
-
-        /* 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 
-         */
-
-        boolean valid = false;
+public class bigIntegerFakultaet {
+    public static void main(String [] args) {
+        
+        // init variables
+        int factorial = 0;
+        BigInteger res = BigInteger.valueOf(1);
+        BigInteger resn = BigInteger.valueOf(1);
 
+        // get factorial input
         try {
-            N = Db.readInt("Gib die Grundzahl als eine Integer-Zahl ein und " +
-                "bestätige mit ENTER ");
+            factorial = Db.readInt("factorial = ");
         } catch (NumberFormatException e) {
+            Db.show("Error: no valid integer input provided");
             System.exit(1);
         }
 
-        //  validate that: N >= 0
-
-        if ( N < 0 ) {
-                Db.show(
-                "Falsche Eingabe: der Input-Parameter N < 0 \n"+
-                "Probieren Sie nochmals! Das Programm ist sofort beendet!");
+        //  validate that: factorial >= 0
+        if (factorial < 0) {
+            Db.show("Error: input cannot be negative");
             System.exit(1);
         }
 
-        // validation prompt
-
-        Db.show(
-            "Die Eingabe ist OK! Sie möchten also " + N + "! berechnen!");
+        // print top table row
+        System.out.format("n \tn!\n\n");
 
         // special case: 0! = 1
-
-        if (N == 0) {
-            System.out.println("Fakultaet  " + N + "! = " + 1);
+        if (factorial == 0) {
+            System.out.format("%d \t%d (%d-digit)\n", 0, 1, res.toString().length());
             System.exit( 0 );
         }
 
-        System.out.format("\n%s \t n! \n-------------------------------------", " n", N);
-
-
         // use big integer
-        BigInteger bigN = BigInteger.valueOf(N);
-        bigN.add(BigInteger.ONE);
-        bigN = bigN.add(BigInteger.ONE);
-        System.out.println(bigN);
-        BigInteger res = BigInteger.valueOf(1);
-        BigInteger resn = BigInteger.valueOf(1);
+        BigInteger bigFactorial = BigInteger.valueOf(factorial);
 
-        for (BigInteger n = BigInteger.valueOf(1); n.compareTo(bigN) < 0; n = n.add(BigInteger.ONE))
+        for (BigInteger n = BigInteger.valueOf(1); n.compareTo(bigFactorial) <= 0; n = n.add(BigInteger.ONE))
         {
             res = n.multiply(resn); // current result
             resn = res; // new overall result
-            System.out.format("\n %2d \t %-20d", n, res);
+            System.out.format("%d \t%d (%d-digit)\n", n, res, res.toString().length());
         }
-
-        System.out.format("\n-------------------------------------");
-
-        // solution prompt
-
-        JOptionPane.showMessageDialog (null,
-            N + "! = " + res,
-            "Lösung", JOptionPane.INFORMATION_MESSAGE); 
     }
 }

+ 24 - 54
inputOutputFlanagan/bigIntegerPotenzen.java

@@ -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());
+        }
     }
 }