Multiplication of big data without BigInteger in Java

Source: Internet
Author: User

Yesterday saw a topic: calculate 1234!, can't use the BigInteger class
It is well known that factorial data can be very large, and the commonly used int and long types are not sufficient at all. Generally think of only BigInteger class, but the topic clearly said can not use, so can only think of other ways.
Factorial is actually the multiplication of recursion, this problem can be simplified to how to achieve the multiplication of big data, int and long type can not fit the data, only string to represent, so as long as the implementation of the two string to represent the multiplication of the number can achieve the topic requirements.
Think of our own hand multiplication of the steps, the basic is a column of a vertical, respectively, by the phase multiplication, the number of rounding together. Just use the program to simulate the process, OK.

When the column is vertical, divide a number into a Chichong bit. is actually equivalent to an integer array: By figuring this out, you can write the code.

Package Cn.baokx;public class Training{public static int [] multi (String str1, String str2) {//Converts the received string into a reversed char array string Buffer buffer = new StringBuffer (); Buffer.append (STR1); char[] nums1 = Buffer.reverse (). toString (). ToCharArray (); Buffer.setlength (0); Buffer.append (STR2); char[] Nums2 = Buffer.reverse (). toString (). ToCharArray ();//pre-declaring an array, Used to hold the result of multiplying the individual digits (similar to column vertical) int len = nums1.length+nums2.length;int [] array = new int[len];//simulate vertical calculation for (int i = 0; i < NUMS2. length; i++) {for (int j = 0; J < Nums1.length; J + +) {array[len-1-(i+j)] + = (nums2[i]-48) * (nums1[j]-48);}} return array;} Carry operations on an array, returning the final result as a string public static string Arrayformat (int [] array) {for (int i = array.length-1; i > 0; i--) {array [I-1] + = array[i]/10;array[i] = array[i]%10;} StringBuffer buffer = new StringBuffer (); if (array[0]!=0) {buffer.append (array[0]);} for (int i = 1; i < Array.Length; i++) {buffer.append (array[i]);} return buffer.tostring ();} Factorial public static string getfactorial (String num) {if ("1". Equals (num)) {REturn "1";} Else{return Arrayformat (multi (Num,getfactorial ((Integer.parseint (num)-1) + "));}} public static void Main (string[] args) {System.out.println (getfactorial ("1234"));//system.out.println (Arrayformat ( multi ("Ten", "ten"));//system.out.println (Arrayformat (multi ("99", "99"));}}


Multiplication of big data without BigInteger in Java

Related Article

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.