Large number templates (Java)

Source: Internet
Author: User

Large number of additions

/*give 2 large integers, a, B, to calculate the results of the a+b. Input line 1th: Large number A 2nd row: Large number B (A, B length <= 10000 need to note: There may be negative) output outputs A + binput example 68932147586468711654886Output example 537643802472 */import java.util.*; import java.io.*; import Java.lang.string;import java.math.BigDecimal; Public classpp{ Public Static voidMain (string[] args) {String s1,s2; Scanner Cin=NewScanner (System.inch); S1=Cin.next (); S2=Cin.next (); BigDecimal B1=NewBigDecimal (S1); BigDecimal B2=NewBigDecimal (S2); System. out. println (B1.add (B2));    Cin.close (); }}
Large number addition -656ms/1s

a=Int (input ()) b=int (input ())print(a+b)
py2-78ms/1s

Large number multiplication

/*give 2 large integers, a, B, to calculate the results of the a*b. Input line 1th: Large number A 2nd line: Large number B (A, a, length <= 1000,a,b >= 0) output outputs A * Binput Example 123456234567Output example 28958703552*/import Java.math.biginteger;import Java.util.Scanner; Public classpp{ Public Static voidMain (string[] args) {Scannerinch=NewScanner (System.inch);        BigInteger A, B; A=inch. Nextbiginteger (); b=inch. Nextbiginteger (); System. out. println (a.multiply (b)); }}
large number multiplication -4515ms/6000

a=Int (raw_input ()) B=int (raw_input ())print a*b
py2-2578ms

Factorial of N

/*Enter N to find the exact value of the factorial of N. Input line 1th: Large number a 2nd row: large number Binput input n (1 <= n <= 10000) output n Factorial input Example 5Output example*/import Java.math.biginteger;import Java.util.Scanner; Public classpp{ Public Static voidMain (string[] args) {Scannerinch=NewScanner (System.inch); BigInteger sum=Biginteger.one; intN; N=inch. Nextint ();  for(intI=1; i<=n;i++) Sum=sum.multiply (biginteger.valueof (i)); System. out. println (sum); }}
factorial of N

Large number Division + modulus

/*give 2 large integers, a, B, to calculate the results of A/b and a mods. Input line 1th: Large number A 2nd row: Large number B (A, a, length <= 100000,a,b >= 0) Output 1th line: A/b 2nd line: A Mod B (a% B) Input Example 9876543211234Output example 800368 209*///2750 ms/4sImportJava.math.BigInteger;ImportJava.util.Scanner; Public classpp{ Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in);        BigInteger A, B; A=In.nextbiginteger (); b=In.nextbiginteger ();        System.out.println (A.divide (b));    System.out.println (A.mod (b)); }}
Large number Division + modulus

Large number templates (Java)

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.