Блог пользователя pawarashish564

Автор pawarashish564, 5 лет назад, По-английски

BigInteger

For handling numbers, Java provides built-in types such as int and long. but thes containers are having some limits. After reaching that limit they throw garbage values . The maximum value of long can store is Long.MAX_VALUE which is 9223372036854775807 . For working with number larger than this we can you BigInteger in java . Java provides built-in class called BigInteger to handle very large number.

Actually BigInteger does not have any limit. It will work until you have space in your random access memory for JVM.

isn't it fascinating ? Let's have a look what we can do with BigInteger class with its various built-in methods.

  • Library : import java.math.BigInteger;

  • Input from stdin : BigInteger bi = sc.nextBigInteger();

  • Constants: BigInteger.ZERO BigInteger.ONE BigInteger.TEN

General Arithmetic :

BigInteger.intValue();
BigInteger.add(BigInteger b); // a + b
BigInteger.subtract(BigInteger b); // a - b
BigInteger.multiply(BigInteger b); // a * b
BigInteger.divide(BigInteger b); // a / b
BigInteger.pow(int p); // a ^ p
BigInteger.remainder(BigInteger m); // a % m
BigInteger.mod(BigInteger m); // a % m
BigInteger.modInverse(BigInteger m); // a^-1 % m
BigInteger.modPow(BigInteger p, BigInteger m); // a^p % m
BigInteger.negate(); // -a
BigInteger.not(); // ~a
BigInteger.and(BigInteger b); // a & b
BigInteger.andNot(BigInteger b); // a & ~b
BigInteger.or(BigInteger b); // a | b
BigInteger.xor(BigInteger b);
BigInteger.shiftLeft(int n); // a << n
BigInteger.shiftRight(int n); // a >> n
BigInteger.max(BigInteger b); // max(a, b)
BigInteger.min(BigInteger b); // min(a, b)
BigInteger.toString(int b); // to base convertor

You can also check the large number is prime or not using isProbablePrime() method BigInteger num = new BigInteger("121020010201001039"); System.out.println(num.isProbablePrime(100)); // true Also if you want next prime after giver number you can use nextProabablePrime() method: System.out.println(num.nextProbablePrime()); //121020010201001057

Large Factorials in java Using BigInteger:

Recursion is some what similar to the way we do it using integers or long but difference we used here the methods to perform operations .

  static BigInteger fastFactorial(int b)
  {
    if(BigInteger.ONE.equals(BigInteger.valueOf(b)))
      return BigInteger.ONE;
    else 
      return BigInteger.valueOf(b).multiply(fastFactorial(b-1));
  }
  public static void main(String[] args)
  {
    System.out.println(fastFactorial(100));
  }

//93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

For More information :Click here

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

Автор pawarashish564, история, 5 лет назад, По-английски

I'm new to codeforces contests. I don't know how the codeforces hacks works. I could not find anything related to these hack except for this youtube video. Can anyone explain me about these hacks. any tutorial or how to hack a solution.

Полный текст и комментарии »

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится