-2

code image

import java.math.BigInteger;

public class GaayuProbOne {
    static void power(int N, int P) {
        BigInteger result = new BigInteger("10");
        BigInteger res = result.pow(P);
        System.out.println(res);
    }

    static double power1(int N, int P) {
        double res =Math.pow(N,P);
        return res;
    }

    public static void main(String[] args) {
        int N = 10;
        double P = 25*power1(10,25);
        System.out.println(P);
        int q = (int) P;
        power(N, q);
    }
}

This code is to calculate 10251025 program in java.
How to calculate it?

Exception in thread "main" java. lang. Arithmetic Exception: Big Integer would overflow supported range

Is there any method to calculate 10251025 in a java program?

VAIBHAV NIRMAL
  • 194
  • 2
  • 13
  • 1
    First hint: `q` isn't what you expect. Try printing it out, and consider the value of 10^25 compared with `Integer.MAX_VALUE`. – Jon Skeet Nov 19 '21 at 08:32
  • Consider using [Java naming conventions](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html). – Abra Nov 19 '21 at 08:37
  • You are not multying 10pow25 with 10pow25 but taking 10 to the power of 10pow25. – dan1st Nov 19 '21 at 08:38
  • 2
    But fundamentally, no, you can't store a value with 2500000000000000000000000000000000000000000000000000 digits in a BigInteger. – Jon Skeet Nov 19 '21 at 08:53
  • As for the supported range of [`BigInterger`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigInteger.html), note the "**Implementation Note**: In the reference implementation, `BigInteger` constructors and operations throw `ArithmeticException` when the result is out of the supported range of `-2^Integer.MAX_VALUE` (exclusive) to `+2^Integer.MAX_VALUE` (exclusive)." See also: https://stackoverflow.com/questions/57874670/does-biginteger-not-have-a-maximum-length-if-it-has-how-can-i-find-the-maximum – Hulk Nov 19 '21 at 09:36

1 Answers1

6

Note: the question has changed significantly since this answer was posted. It originally asked for a way of computing 10^25 x 10^25.

The code has multiple problems:

  • It uses unconventional variable names - not a bug, but still something to fix
  • The power method ignores the value of N
  • You're performing floating point arithmetic for no obvious reason (using Math.pow)
  • You're multiplying by 25, which occurs nowhere in what you're trying to achieve
  • You're casting a very, very large number (25 * 10^25) to int, when the maximum value of int is just 2147483647
  • You're trying to compute 10^2147483647 in the last line - without the previous problem, you'd be trying to compute 10^(25*10^25), which definitely isn't what is specified

The actual code you need is significantly simpler:

  • Find 10^25, as a BigInteger
  • Multiply that by itself

Simple code to do that:

import java.math.BigInteger;

public class Test {
    public static void main(String[] args) {
        int n = 10;
        int p = 25;

        BigInteger tmp = BigInteger.valueOf(n).pow(p);
        BigInteger result = tmp.multiply(tmp);
        System.out.println(result);
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    @VAIBHAVNIRMAL: It's incredibly disrespectful to completely change the aim of the question. I would request that you revert the change, and then ask a new question with what you're *actually* trying to achieve. Not taking sufficient care over what you posted in the first place has wasted the time of everyone trying to help you, and now the change invalidates this post which answers *the question that was asked*. – Jon Skeet Nov 19 '21 at 08:51
  • The original text of the question contained 10^25*10^25 which I interpreted as _ten to the power of 25 multiplied by ten to the power of 25_ and which I may have misinterpreted. – Abra Nov 19 '21 at 09:04