Demonstration of high-precision integer arithmetic with the BigInteger class : BigInteger « Data Type « Java






Demonstration of high-precision integer arithmetic with the BigInteger class

Demonstration of high-precision integer arithmetic with the BigInteger class
   
/*
Java Programming for Engineers
Julio Sanchez
Maria P. Canton


ISBN: 0849308100
Publisher: CRC Press
*/


// Java for Engineers
//Filename: BigIFact
//Reference: Chapter 22
//Description:
//         Demonstration of high-precision integer arithmetic
//         with the BigInteger class. Program calculates the
//         factorial of a big integer number
//Requires:
//         Keyin class in current directory

import java.math.BigInteger;

public class BigIFact {
  public static void main(String[] args) {

    int v; // Input
    BigInteger p = BigInteger.valueOf(1); // Factor

    System.out.println("Big integer factorial routine\n");
    v = Keyin.inInt("Enter value: ");

    // Calculate factorial by iteration
    for (int i = 1; i <= v; i++)
      p = p.multiply(BigInteger.valueOf(i));
    // Display result
    System.out.println(p);
  }
}

//**********************************************************
//**********************************************************
//Program: Keyin
//Reference: Session 20
//Topics:
//1. Using the read() method of the ImputStream class
//in the java.io package
//2. Developing a class for performing basic console
//input of character and numeric types
//**********************************************************
//**********************************************************

class Keyin {

  //*******************************
  //   support methods
  //*******************************
  //Method to display the user's prompt string
  public static void printPrompt(String prompt) {
    System.out.print(prompt + " ");
    System.out.flush();
  }

  //Method to make sure no data is available in the
  //input stream
  public static void inputFlush() {
    int dummy;
    int bAvail;

    try {
      while ((System.in.available()) != 0)
        dummy = System.in.read();
    } catch (java.io.IOException e) {
      System.out.println("Input error");
    }
  }

  //********************************
  //  data input methods for
  //string, int, char, and double
  //********************************
  public static String inString(String prompt) {
    inputFlush();
    printPrompt(prompt);
    return inString();
  }

  public static String inString() {
    int aChar;
    String s = "";
    boolean finished = false;

    while (!finished) {
      try {
        aChar = System.in.read();
        if (aChar < 0 || (char) aChar == '\n')
          finished = true;
        else if ((char) aChar != '\r')
          s = s + (char) aChar; // Enter into string
      }

      catch (java.io.IOException e) {
        System.out.println("Input error");
        finished = true;
      }
    }
    return s;
  }

  public static int inInt(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Integer.valueOf(inString().trim()).intValue();
      }

      catch (NumberFormatException e) {
        System.out.println("Invalid input. Not an integer");
      }
    }
  }

  public static char inChar(String prompt) {
    int aChar = 0;

    inputFlush();
    printPrompt(prompt);

    try {
      aChar = System.in.read();
    }

    catch (java.io.IOException e) {
      System.out.println("Input error");
    }
    inputFlush();
    return (char) aChar;
  }

  public static double inDouble(String prompt) {
    while (true) {
      inputFlush();
      printPrompt(prompt);
      try {
        return Double.valueOf(inString().trim()).doubleValue();
      }

      catch (NumberFormatException e) {
        System.out
            .println("Invalid input. Not a floating point number");
      }
    }
  }
}
           
         
    
    
  








Related examples in the same category

1.BigInteger.isProbablePrime
2.Compute the Palindrome of a number by adding the number composed of
3.Operating with Big Integer Values
4.Create BigInteger via string
5.Create BigInteger via long type variable
6.Multiply one BigInteger to another BigInteger
7.Subtract one BigInteger from another BigInteger
8.Divide one BigInteger from another BigInteger
9.Negate a BigInteger
10.Calculate the power on a BigInteger
11.Create BigInteger from byte array
12.Performing Bitwise Operations with BigInteger
13.Get the value of a bit
14.Set a bit for BigInteger
15.Clear a bit in a BigInteger
16.Flip a bit in a BigInteger
17.Shift the bits in a BigInteger
18.Shift right in a BigInteger
19.xor a BigInteger
20.and operation on BigInteger
21.not operation for BigInteger
22.or operation for BigInteger
23.antNot operation on BigInteger
24.Retrieve the current bits in a byte array in twos-complement form.
25.Parsing and Formatting a Big Integer into Binary
26.Parsing and Formatting a Big Integer into octal
27.Parsing and Formatting a Big Integer into decimal
28.Parse and format to hexadecimal
29.Parse and format to arbitrary radix <= Character.MAX_RADIX
30.Create a BigInteger using the byte array
31.Parsing and Formatting a Byte Array into Binary
32.Parsing and Formatting a Byte Array into Octal
33.Parsing and Formatting a Byte Array into decimal
34.Parsing and Formatting a Byte Array into Hexadecimal
35.Parse binary string
36.Get byte array from BigInteger
37.Parse octal string to create BigInteger
38.Parse decimal string to create BigInteger
39.Parse hexadecimal string to create BigInteger
40.Convert BigInteger into another radix number
41.Do math operation for BigInteger
42.Operate with big integer values in code
43.This program uses big numbers to compute the odds of winning the grand prize in a lottery.
44.BigInteger bitwise Gcd
45.BigInteger lowest numBits
46.BigInteger highest numBits
47.Is one BigInteger Divisible by another BigInteger
48.Is a BigInteger Even