当我分裂时,为什么我乘以许多数字会犯错误?

时间:2017-03-02 11:16:38

标签: java algorithm division

这是我的结果:

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

可分2:真实 可分的3:假的 可分的5:假
可分的7:假的 可分的11:真实

这是我的代码:

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;
}

任何人都可以解释为什么会这样吗?谢谢:))

4 个答案:

答案 0 :(得分:6)

根据wolfram alpha,结果是

117288381359406970983270

但是在java中,int可以存储的最大数字是

2147483647

即使是长整数,也就是64位只能容纳

9223372036854775807

要解决这个问题,你需要使用某种形式的大数字类。有关这方面的一些方法,请参阅this question

总结这个链接的问题,您可以使用java.math中的大整数类来解决这个问题:

BigInteger result = new BigInteger("117288381359406970983270");

它还包括添加和乘以BigIntegers的函数。

答案 1 :(得分:1)

您可能会使用错误的数据类型来存储结果,因为

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

您可以切换到Java中的BigIntegers

这是一个有效的java代码:

<强>输出

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

<强>代码

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"));
    }
}

答案 2 :(得分:0)

因为3982538761641808742 - 结果错误。 因为长类型不足以存储正确的结果。

答案 3 :(得分:-1)

此结果:3982538761641808742取决于某个类型的值:

  

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

如果那些是整数然后你得到一个溢出,因为结果不能保持在32位,甚至不接近 3982538761641808742 ......

如果你将它们定义为长操作,那么

3982538761641808742

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

无论如何,3982538761641808742

  • 除以2得不到int所以可分为2:true
  • 除以2得不到int所以可分为3:false
  • 除以2得不到int这样可分为5:false
  • 除以2得不到int所以可分为7:false
  • 除以2得不到int这样可分的11:true

这是正确的......

相关问题