(partially reproduced, partially original) Java large number class (1)

Source: Internet
Author: User

Header file:

Import java.io.*;

Import java.util.*;

Import java.math.*;

BigInteger belongs to Java.math.BigInteger

Read in:

Scanner cin = Scanner (system.in);

while (Cin.hasnext ())//equivalent to!=eof

N=cin.nextint (x);//read in an int type X-binary number, general x default, default decimal

N=cin.nextbiginteger (x);//Read into an X-binary large integer, general x default, default decimal

Data type:

BigInteger arbitrarily large integers, in principle, as long as your computer's memory is large enough to have unlimited digits

BigDecimal arbitrary large real numbers, can handle decimal precision problems.

Some common constants in BigInteger are:

Biginteger.zero, Biginteger.one, Biginteger.ten

Assign the number of int to BigInteger:

Biginteger.valueof (k);

Define an X-binary BigInteger:

BigInteger a = new Bigteger ("123", X);

x defaults to decimal by default

Basic functions:

ValueOf (): Assign initial value

Add (): + A.add (b);

Subtract ():-

Multiply (): *

Divide ():/

Remainder (): this% val

Divideandremainder (): A[0]=this/val; A[1]=this% val

Pow (): A.pow (b) =a^b

GCD (), ABS (): Number of conventions, absolute

Negate (): Take negative numbers

Signum (): Symbolic function

MoD (): A.mod (b) =a%b;

Shiftleft (): Shift left, this << N, this*2^n;

Shiftright (): Shift right, this >> n,this/2^n;

and (): &&

or (): | |, or;

XOR (): ^, XOR

Not ():!, non;

Bitlength (): Returns the number of bits represented by the smallest twos complement of this digit, that is, * does not include the * sign bit (ceil (log2 (this <0?-this:this + 1)). For positive numbers, this is equivalent to the number of bits in the normal binary representation.

Bitcount (): Returns the number of bits in the binary complement representation of the digit that do not pack the spread symbol bit. This method is useful for implementing bit-vector-style collections on top of bigintegers.

Isprobableprime (): Returns True if the BigInteger is likely to be a prime number, or False if it is explicitly a composite. Parameter certainty is a measure of uncertainty that the caller is willing to endure: if the probability of the number being a prime is greater than the 1-1/2**certainty method, the method returns True. The execution time is proportional to the value of the parameter certainty.

CompareTo (): Based on the value is less than, equal to, or greater than Val returns-1, 0, or 1;

Equals (): Judge whether two number is equal, can also use CompareTo to replace;

Min (), Max (): Take two numbers of smaller, larger ones;

Intvalue (), Longvalue (), Floatvalue (), double (): Converts the number to the value of the number of that type.

ToString (x): You should use the. ToString (x) method to convert the result of the calculation to an X-binary string, usually x default, and the default is decimal.

Type conversions:

BigInteger int:

int a = 3;

BigInteger B = biginteger.valueof (a);

BigInteger-INT:

BigInteger a = Biginteger.ten;

int b = A.intvalue ();

String int:

int a = 3;

String B = a.tostring ();

String-INT:

String a = "123";

int B = Intrger.parseint (a);

(also Float.parsefloat (), similar in usage)

String-BigInteger:

String a = "123";

BigInteger B = New BigInteger (a);

BigInteger, String:

BigInteger a = Biginteger.ten;

String B = a.tostring ();

Binary conversions:

10 binary, 16 binary:

Integer.tohexstring (4);

10 binary, 2 binary:

Integer.tobinarystring (4);

16 Binary, 10 binary:

Integer.parseint ("4", 16);

Attention:

About the difference between remainder and mod :

BigInteger remainder(BigInteger val)
Similar to mod, the difference is that remainder is the take-rest operation, MoD is modulo operation, the specific difference is as follows:

(partially reproduced, partially original) Java large number class (1)

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.