High-precision arithmetic Java algorithm

Source: Internet
Author: User

Problem description
Enter two integers a and b to output the and of the two integers. A and B are not more than 100 bits.
Algorithm description
Because both A and B are large, they cannot be stored directly using the standard data types in the language. For this type of problem, arrays are generally used for processing.
Define an array a,a[0] to store A's bits, a[1] to store A's 10 bits, and so on. You can also use an array B to store B.
When calculating C = A + B, first a[0] and b[0] are added, and if there is a carry, then the rounding (that is, the sum of 10 digits) is stored in R, and the single digit of the sum is deposited in c[0], i.e. c[0] equals (a[0]+b[0])%10. Then calculate a[1] and b[1] Add, at this time also should be low into the value of R also add up, that is c[1] should be a[1], b[1] and R three number of the sum. If another carry is generated, the new rounding can still be deposited into r, and the bits are stored in c[1]. And so on, you can find all the bits of C.
Finally, c output can be.
Input format
The input consists of two lines, the first behaves a nonnegative integer A, and the second behaves as a non-negative integer b. Two integers are not more than 100 bits, and the highest bit of two numbers is not 0.
Output format
Outputs a row that represents the value of a + B.
Sample input
20100122201001221234567890
2010012220100122
Sample output

20100122203011233454668012

You can use the Java BigDecimal class, the code is as follows

Import Java.math.BigDecimal;  Import Java.util.Scanner;    public class Main {        private static BigDecimal a,b,c;      public static void Main (string[] args) {          Scanner sc = new Scanner (system.in);          A = Sc.nextbigdecimal ();          b = Sc.nextbigdecimal ();          c = A.add (b);//plus  //        C = a.subtract (b);/  /minus//        C = a.multiply (b);//Multiply  //        C = a.divide (b);//except C14/>system.out.println (c);      }    }  

Attached: High accuracy factorial

Method One: Starting from 1, increment by n

Import Java.math.BigDecimal;    Import Java.util.Scanner;    public class Test {public        static BigDecimal factorial (int n) {             BigDecimal result = new BigDecimal (1);            BigDecimal A;            for (int i = 2; I <= n; i++) {                a = new BigDecimal (i);//Convert I to BigDecimal type                result = Result.multiply (a);//No Result*a Because the BigDecimal type does not define * operation           }            return result        ;                public static void Main (string[] arguments) {            Scanner sc = new Scanner (system.in);            int a = Sc.nextint ();//Read the console input integer            System.out.println (A + "! =" + factorial (a));}        }    

Method Two: Starting from N, decreasing by 2

Import Java.math.BigDecimal;    Import Java.util.Scanner;    public class Main {public        static BigDecimal factorial (BigDecimal N) {            BigDecimal bd1 = new BigDecimal (1);//bigdec iMAL Type 1            BigDecimal bd2 = new BigDecimal (2),//bigdecimal type 2           BigDecimal result = bd1;//results set, initial value takes 1 while            ( N.compareto (BD1) > 0) {//parameter greater than 1, enter loop                result = Result.multiply (n.multiply (N.subtract (BD1)));//Implement result* (n (n-1))                n = n.subtract (BD2);//n-2 continues after            }            return result;        public static void Main (string[] arguments) {            Scanner sc = new Scanner (system.in);            BigDecimal  n = sc.nextbigdecimal ();                        System.out.print (n + "! =" + factorial (n));}        }    

Method Three: Recursive implementation: F (n) =n*f (n-1), f (1) = 1, recursive multiplication

Import Java.util.Scanner;    Import Java.math.BigDecimal;    public class Factorial {public        static BigDecimal factorial (BigDecimal N) {            BigDecimal bd1 = new BigDecimal (1);//1            if (n.equals (new BigDecimal (1))) {                return bd1;            }            else                return n.multiply (factorial (n.subtract (BD1))),//n*f (n-1)        } public        static void Main (string[] Arguments) {            Scanner sc = new Scanner (system.in);            BigDecimal a = Sc.nextbigdecimal ();            BigDecimal result = factorial (a);               System.out.println (A + "! =" +result);}        }    

  

 

  

High-precision arithmetic Java algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.