-6

This is my result:

2 x 3 x 5 x 7 x 11 x 13 x 17 x 19 x 23 x 29 x 31 x 37 x 41 x 43 x 47 x 53 x 59 x 61 = 3,982,538,761,641,808,742

divisable 2 : true
divisable 3 : false
divisable 5 : false
divisable 7 : false
divisable 11 : true

this is my code :

public static void main(String[] args) {
    final ArrayList<Long> prime = prime();
    System.out.println("size : " + prime.size());

    long i = 1;
    for (Long l : prime) {
        System.out.print(" x "+l);
        i *= l;
    }
    System.out.println(" = "+i);
    long a[] = {2, 3, 5, 7, 11};
    for (long b : a) {
        System.out.printf("divisable %d : %b %n", b, i % b == 0 ? true : false);
    }
}

public static ArrayList<Long> prime() {
    ArrayList<Long> res = new ArrayList<>();
    res.add(new Long(2));

    next:
    for (long i = 3; i < Byte.MAX_VALUE / 2; i += 2) {
        //make Byte.MAX_VALUE / 3 will return true;
        for (Long l : res) {
            boolean b = true;
            if (i % l == 0) {
                continue next;
            }
        }

        res.add(i);

    }

    return res;
}

Can anyone please explain why is that? Thank you :)

4 Answers4

6

According to wolfram alpha, the result is

117288381359406970983270

However in java the largest number an int can store is

2147483647

And even a long integer, which is 64 bits can only hold

9223372036854775807

To get around this, you will need to use some form of big number class. See this question for some ways of doing so.

To summarise that linked question, you can use the big integer class from java.math to get around this:

BigInteger result = new BigInteger("117288381359406970983270");

It also includes functions to add and multiply BigIntegers.

Community
  • 1
  • 1
Matt
  • 968
  • 2
  • 13
  • 22
  • what about speed of BigInteger ? How does it works ? :p – Thang Phung Mar 02 '17 at 11:42
  • Yes, using BigInteger will be slower than using regular data types, but there is no way to avoid it using numbers that big. To make a BigInteger, use the syntax in my example. For multiplication, you can use the BigInteger.multiply() method, which takes another BigInteger as an argument, and returns the products as a BigInteger. – Matt Mar 02 '17 at 11:48
  • @ThangPhung You have to pay some price. – Jean-Baptiste Yunès Mar 02 '17 at 12:32
1

You're possibly taking wrong data type to store result because

2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 * 41 * 43 * 47 * 53 * 59 * 61
=117288381359406970983270

You can switch to BigIntegers in Java

Here's a working java code:

Output

117288381359406970983270
Divisible by 2 : true
Divisible by 3 : true
Divisible by 5 : true
Divisible by 7 : true
Divisible by 11 : true

Code

import java.math.BigInteger;

public class HelloWorld {
    public static void main(String[] args) {
        int arr[] = {2,  3,  5,  7,  11,  13,  17,  19,  23,  29,  31,  37,  41,  43,  47,  53,  59,  61};

        BigInteger bi = new BigInteger("1");
        for (int i = 0; i < arr.length; i++) {
            bi = bi.multiply(new BigInteger(Integer.toString(arr[i])));
        }

        System.out.println(bi);

        System.out.println("Divisible by 2 : " + bi.mod(new BigInteger("2")).toString().equals("0"));
        System.out.println("Divisible by 3 : " + bi.mod(new BigInteger("3")).toString().equals("0"));
        System.out.println("Divisible by 5 : " + bi.mod(new BigInteger("5")).toString().equals("0"));
        System.out.println("Divisible by 7 : " + bi.mod(new BigInteger("7")).toString().equals("0"));
        System.out.println("Divisible by 11 : " + bi.mod(new BigInteger("11")).toString().equals("0"));
    }
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

Because 3982538761641808742 - is wrong result. Because long type is not long enough to store right result.

Pavlo Plynko
  • 586
  • 9
  • 27
-1

this result: 3982538761641808742 is depending of what are this values for a type:

2 x 3 x 5 x 7 x 11 x 13 x 17 x 19 x 23 x 29 x 31 x 37 x 41 x 43 x 47 x 53 x 59 x 61

if those are ints then you get an overflow since the result can not be hold in 32 bits and is not even close to 3982538761641808742 ...

if you define those as a long operation then

3982538761641808742 is the result of

final long n0 = 2L * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 * 41 * 43 * 47 * 53 * 59 * 61;

no matter what, 3982538761641808742

  • divided by 2 in not resulting an int so divisable 2 : true
  • divided by 2 in not resulting an int so divisable 3 : false
  • divided by 2 in not resulting an int so divisable 5 : false
  • divided by 2 in not resulting an int so divisable 7 : false
  • divided by 2 in not resulting an int so divisable 11 : true

which is correct...

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97