Showing posts with label BigInteger class. Show all posts
Showing posts with label BigInteger class. Show all posts

BigInteger class Method in Java Part - II

java.math.BigInteger

Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java'sprimitive integer types). It provides analogs to all of the Java'sprimitive integer operators, and all relevant methods from java.lang.Math

BigInteger provides operations for modular arithmetic, GCDcalculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Some methods of BigInteger class

negate(): It returns a BigInteger whose value is (-this).


nextProbablePrime(): It returns the first integer greater than this BigInteger that is probably prime.


not(): It returns a BigInteger whose value is (~this). 


BigInteger.ONE: The BigInteger constant one.


or(): It returns a BigInteger whose value is (this | val).


pow(): It returns a BigInteger whose value is (this^exponent). 


probablePrime(): It returns a positive BigInteger that is probably prime, with the specified bitLength. 


remainder(): It returns a BigInteger whose value is (this % val).


setBit(): It returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. (Computes (this | (1<<n)).)


shiftLeft(): It returns a BigInteger whose value is (this << n).


shiftRight(): It returns a BigInteger whose value is (this >> n).


shortValue(): It returns the value of the specified number as a short.


shortValueExact(): It converts this BigInteger to a short, checking for lost information. If the value of this BigIntegeris out of the range of the short type, then an ArithmeticException is thrown.


signum(): It returns the signum function of this BigInteger.


sqrt(): It returns the integer square root of this BigInteger.


sqrtAndRemainder(): It returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively.


subtract():  It returns a BigInteger whose value is (this - val).


BigInteger.TEN: The BigInteger constant ten.


testBit(): his method returns true if and only if the designated bit is set.(Computes ((this &(1<<n)) != 0).)


toByteArray(): This method returns a byte array containing the two's-complement representation of this BigInteger.


toString(): It returns the decimal String representation of this BigInteger.


toString(int radix): It returns the string representation of this BigInteger in the given radix.


BigInteger.TWO: The BigInteger constant two.


BigInteger.valueOf(): It returns a BigInteger whose value is equal to that of the specified long.


xor(): It eturns a BigInteger whose value is (this ^ val).


BigInteger.ZERO: The BigInteger constant is zero.


Some More Methods of BigInteger Class


BigInteger.ZERO in Java

BigInteger.ZERO: This is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.ZERO

The BigInteger constant is zero.

Approach

Java

import java.math.BigInteger;

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

        System.out.println(BigInteger.ZERO);
    }
}

Output:

0

BigInteger xor() in Java

xor(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.xor(BigInteger val)

This method takes one argument of type BigInteger as its parameter. This method returns a BigInteger whose value is (this ^ val). (This method returns a negative BigInteger if and only if exactly one of this and val are negative.)

Parameters: One parameter is required for this method.

val: value to be XOR'ed with this BigInteger.

Returns: this ^ val

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger val = new BigInteger("123")

bigInteger.xor(val) = > It returns 1193

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");

        BigInteger val = new BigInteger("123");
        System.out.println(bigInteger.xor(val));
    }
}

Output:

1193

BigInteger.valueOf() in Java

BigInteger.valueOf() : This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.valueOf(long val)

This method takes one argument of type long as its parameter. This method returns a BigInteger whose value is equal to that of the specified long.

Parameters: One parameter is required for this method.

val: the value of the BigInteger to return.

Returns: a BigInteger with the specified value.

For Example:

long val = 91991

BigInteger.valueOf(val) = > It returns 91991.

Approach

Java


import java.math.BigInteger;

public class BigIntegervalueOf {

    public static void main(String[] args) {

        long val = 91991;
        System.out.println(BigInteger.valueOf(val));

    }
}

Output:

91991

BigInteger.TWO in Java

BigInteger.TWO: This is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.TWO

The BigInteger constant two.

Approach

Java

import java.math.BigInteger;

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

        System.out.println(BigInteger.TWO);
    }
}

Output:

2

BigInteger toString() Radix in Java

toString(): This method is available in java.math.BigInteger class of Java.

Syntax:

String java.math.BigInteger.toString(int radix)

This method takes one argument of type int as its parameter. This method returns the string representation of this BigInteger in the given radix. If the radix is outside the range from Character.MIN_RADIX to Character.MAX_RADIX inclusive,it will default to 10.

Parameters: One parameter is required for this method.

radix: radix of the String representation.

Returns: String representation of this BigInteger in the given radix.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int radix = 2

bigInteger.toString(radix) = > It returns 10011010010.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        int radix = 2;
        System.out.println(bigInteger.toString(radix));
    }
}

Output:

10011010010

BigInteger toString() in Java

toString(): This method is available in java.math.BigInteger class of Java.

Syntax:

String java.math.BigInteger.toString()

This method returns the decimal String representation of this BigInteger.The digit-to-character mapping provided by Character.forDigit is used, and a minus sign is prepended if appropriate. 

Returns: decimal String representation of this BigInteger.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.toString()  = > It returns 1234.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.toString());
    }
}

Output:

1234

BigInteger toByteArray() in Java

toByteArray(): This method is available in java.math.BigInteger class of Java.

Syntax:

byte[] java.math.BigInteger.toByteArray()

This method returns a byte array containing the two's-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element.

Returns: a byte array containing the two's-complement representation of this BigInteger.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.toByteArray() = > It returns [4, -46].

Approach

Java

import java.math.BigInteger;
import java.util.Arrays;

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

        BigInteger bigInteger = new BigInteger("1234");
        byte[] arr = bigInteger.toByteArray();

        System.out.println(Arrays.toString(arr));
    }
}

Output:

[4, -46]

BigInteger testBit() in Java

testBit(): This method is available in java.math.BigInteger class of Java.

Syntax:

boolean java.math.BigInteger.testBit(int n)

This method takes one argument of int as its parameter. This method returns true if and only if the designated bit is set.(Computes ((this &(1<<n)) != 0).)

Parameters: One parameter is required for this method.

n: index of the bit to test.

Returns: true if and only if the designated bit is set..

Throws: ArithmeticException - n is negative.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int n = 4

bigInteger.testBit(n) = > It returns true.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        int n = 4;
        System.out.println(bigInteger.testBit(n));
    }
}

Output:

true

BigInteger.TEN in Java

BigInteger.TEN: This is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.TEN

The BigInteger constant ten.

Approach

Java

import java.math.BigInteger;

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

        System.out.println(BigInteger.TEN);
    }
}

Output:

10

BigInteger subtract() in Java

subtract(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.subtract(BigInteger val)

This method takes one argument of type BigInteger as its parameter. This method returns a BigInteger whose value is (this - val).

Parameters: val value to be subtracted from this BigInteger.

Returns: this - val

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger val = new BigInteger("345")

bigInteger.subtract(val) = > It returns 889.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");

        BigInteger val = new BigInteger("345");
        System.out.println(bigInteger.subtract(val));
    }
}

Output:

889

BigInteger sqrtAndRemainder() in Java

sqrtAndRemainder(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger[] java.math.BigInteger.sqrtAndRemainder()

This method returns an array of two BigIntegers containing the integer square root s of this and its remainder this - s*s, respectively. 

Returns: An array of two  BigIntegers with the integer square root at offset 0 and the remainder at offset 1.

Throws: ArithmeticException - if this is negative. (The square root of a negative integer val is (i * sqrt(-val)) where i is the imaginary unit and is equal to sqrt(-1).).

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.sqrtAndRemainder() = > It returns {35,9}.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");

        BigInteger[] arr = bigInteger.sqrtAndRemainder();

        System.out.println(arr[0] + " " + arr[1]);
    }
}

Output:

35 9

BigInteger sqrt() in Java

sqrt(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.sqrt()

This method returns the integer square root of this BigInteger. The integer square root of the corresponding mathematical integer n is the largest mathematical integer s such that s*s <= n. It is equal to the value of floor(sqrt(n)), where sqrt(n) denotes the real square root of n treated as a real.

Returns: the integer square root of this.

Throws: ArithmeticException - if this is negative. (The square root of a negative integer val is (i * sqrt(-val)) where i is the imaginary unit and is equal to sqrt(-1).)

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.sqrt() = > It returns 35.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.sqrt());
    }
}

Output:

35

BigInteger signum() in Java

signum(): This method is available in java.math.BigInteger class of Java.

Syntax:

int java.math.BigInteger.signum()

This method returns the signum function of this BigInteger.

Returns: -1, 0, or 1 as the value of this BigInteger is negative, zero, or positive.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.signum() = > It returns 1.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.signum());

    }
}

Output:

1

BigInteger shortValueExact() in Java

shortValueExact(): This method is available in java.math.BigInteger class of Java.

Syntax:

short java.math.BigInteger.shortValueExact()

This method converts this BigInteger to a short, checking for lost information. If the value of this BigIntegeris out of the range of the short type, then an ArithmeticException is thrown.

Returns: this BigInteger converted to a short.

Throws: ArithmeticException - if the value of this will not exactly fit in a short.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.shortValueExact() = > It returns 1234.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.shortValueExact());
    }
}

Output:

1234

BigInteger shortValue() in Java

shortValue(): This method is available in java.math.BigInteger class of Java.

Syntax:

short java.lang.Number.shortValue()

This method returns the value of the specified number as a short. This implementation returns the result of intValue cast to a short.

Returns: the numeric value represented by this object after conversion to type short.

For Example:

BigInteger bigInteger = new BigInteger("1234")

bigInteger.shortValue() = > It returns 1234.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        System.out.println(bigInteger.shortValue());
    }
}

Output:

1234

BigInteger shiftRight() in Java

shiftRight(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.shiftRight(int n)

This method takes one argument of type int as its parameter. This method returns a BigInteger whose value is (this >> n). Sign extension is performed. The shift distance, n, may be negative, in which case this method performs a left shift.(Computes floor(this / 2n).)

Parameters: One parameter is required for this method.

n: shift distance, in bits.

Returns: this >> n.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int n = 2

bigInteger.shiftRight(n) = > It returns 308.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");

        int n = 2;
        System.out.println(bigInteger.shiftRight(n));
    }
}

Output:

308

BigInteger shiftLeft() in Java

shiftLeft(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.shiftLeft(int n)

This method takes one argument of type int as its parameter. This method returns a BigInteger whose value is (this << n). The shift distance, n, may be negative, in which case this method performs a right shift.(Computes floor(this * 2n).).

Parameters: One parameter is required for this method.

n: shift distance, in bits.

Returns: this << n.

For Example:

BigInteger bigInteger = new BigInteger("1234") 

int n = 3

bigInteger.shiftLeft(n) = > It returns 9872.

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("1234");
        int n = 3;
        System.out.println(bigInteger.shiftLeft(n));
    }
}

Output:

9872

BigInteger setBit() in Java

setBit(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger java.math.BigInteger.setBit(int n)

This method takes one argument of type int as its parameter. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. (Computes (this | (1<<n)).)

Parameters: One parameter is required for this method.

n: index of the bit to set.

Returns: this | (1<<n).

Throws: ArithmeticException - n is negative.

For Example:

BigInteger bigInteger = new BigInteger("123991194")

int n = 6

bigInteger.setBit(n) = > It returns 123991258

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("123991194");

        int n = 6;
        System.out.println(bigInteger.setBit(n));
    }
}

Output:

123991258