0

when i try to compile this program, it gave an error integer number too large: 11111111111. how to correct this ?

class PrimeFactor {
    public static void main(String[] args) {
        int primeLimit = 11111111111;
        int sum = 1;
        for(int i = 1; i < primeLimit + 1; i++) {
            if((primeLimit%i) == 0) {
                sum *= i;
                System.out.println(i);
            }
            if(sum == primeLimit) {
                break;
            }
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
ccc
  • 370
  • 5
  • 19

2 Answers2

1

You can use the long primitive type, if your number is not too large. Long uses twice the number of bits int uses and can represent integral numbers up to Long.MAX_VALUE = 2^63-1:

long primeLimit = 11111111111L;

Of course you have to change the types of other variables, e.g. i and sum, too.

If you want to use larger numbers, use BigInteger.

BigInteger primeLimit = new BigInteger("11111111111111111111111");

If you want to use BigInteger, the normal arithmetic operators won't work anymore. You have to use the methods of BigInteger instead.

fabian
  • 80,457
  • 12
  • 86
  • 114
0

A signed integer can hold values as large as 2^31 - 1, which is 2147483647. Using a long or some other alternative will work.

novic3
  • 106
  • 5