为什么我乘很多数除法时会出错?
why i multiply many number make a mistake when i 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 : true
整除 3 : false
整除 5 : false
整除 7 : false
整除 11 : true
这是我的代码:
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;
}
谁能解释一下这是为什么?谢谢:)
因为 3982538761641808742 - 是错误的结果。
因为 long 类型不够长,无法存储正确的结果。
根据wolfram alpha,结果是
117288381359406970983270
但是在 java 中 int 可以存储的最大数字是
2147483647
而且即使是64位的长整数也只能容纳
9223372036854775807
要解决这个问题,您需要使用某种形式的大数字 class。有关这样做的一些方法,请参阅 this question。
为了总结那个相关问题,您可以使用 java.math 中的大整数 class 来解决这个问题:
BigInteger result = new BigInteger("117288381359406970983270");
它还包括加法和乘法 BigIntegers 的函数。
您可能使用了错误的数据类型来存储结果,因为
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"));
}
}
此结果: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 不会导致整除 3 : false
- 除以 2 不会导致整除 5 : false
- 除以 2 不会导致整除 7 : false
- 除以 2 不会导致整除 11 : true
哪个是正确的...
这是我的结果:
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 : true
整除 3 : false
整除 5 : false
整除 7 : false
整除 11 : true
这是我的代码:
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;
}
谁能解释一下这是为什么?谢谢:)
因为 3982538761641808742 - 是错误的结果。 因为 long 类型不够长,无法存储正确的结果。
根据wolfram alpha,结果是
117288381359406970983270
但是在 java 中 int 可以存储的最大数字是
2147483647
而且即使是64位的长整数也只能容纳
9223372036854775807
要解决这个问题,您需要使用某种形式的大数字 class。有关这样做的一些方法,请参阅 this question。
为了总结那个相关问题,您可以使用 java.math 中的大整数 class 来解决这个问题:
BigInteger result = new BigInteger("117288381359406970983270");
它还包括加法和乘法 BigIntegers 的函数。
您可能使用了错误的数据类型来存储结果,因为
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"));
}
}
此结果: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 不会导致整除 3 : false
- 除以 2 不会导致整除 5 : false
- 除以 2 不会导致整除 7 : false
- 除以 2 不会导致整除 11 : true
哪个是正确的...