简体   繁体   English

如何在Java中获取表大小2 ^ 32

[英]How to get table size 2^32 in java

I have to get in java protected final static int [] SIEVE = new int [ 1 << 32 ]; 我必须进入java protected final static int [] SIEVE = new int [ 1 << 32 ]; But i cant force java to that. 但是我不能强迫Java这么做。

Max sieve what i get is 2^26 i need 2^32 to end my homework. 我得到的最大筛子是2 ^ 26我需要2 ^ 32才能结束作业。 I tried with mask but i need to have SIEVE[n] = k where min{k: k|n & k >2}. 我尝试使用遮罩,但是我需要SIEVE [n] = k,其中min {k:k | n&k> 2}。

EDIT I need to find Factor numbers from 2 to 2^63-1 using Sieve and sieve must have information that P[n]= is smallest prime with divide n. 编辑我需要使用Sieve查找2到2 ^ 63-1的因数,并且sieve必须具有P [n] =是除以n的最小素数的信息。 I know that with sieve i can Factorise number to 2^52. 我知道用筛子可以将因子分解为2 ^ 52。 But how do that exercises with holding on to the content. 但是,如何坚持内容呢?

EDIT x2 problem solved 编辑x2问题已解决

You can't. 你不能 A Java array can have at most 2^31 - 1 elements because the size of an array has to fit in a signed 32-bit integer. 一个Java阵列可以具有至多 2^31 - 1元件,因为size的阵列具有适合在一个符号的32位整数。

This applies whether you run on a 32 bit or 64 bit JVM. 无论您是在32位还是64位JVM上运行,这都适用。


I suspect that you are missing something in your homework. 我怀疑您的作业中缺少一些东西。 Is the requirement to be able to find all primes less than 2^32 or something? 是否要求能够找到所有小于2^32素数? If that is the case, they expect you to treat each int of the int[] as an array of 32 bits. 如果是这种情况,他们希望您将int[]每个int视为32位数组。 And you need an array of only 2^25 ints to do that ... if my arithmetic is right. 如果我的算术是正确的,则只需要一个2^25 int的数组即可。

A BitSet is another good alternative. BitSet是另一个不错的选择。

A LinkedList<Integer> is a poor alternative. LinkedList<Integer>是一个较差的选择。 It uses roughly 8 times the memory that an array of the same size would, and the performance of get(int) is going to be horribly slow for a long list ... assuming that you use it in the obvious fashion. 它使用的内存大约是相同大小的数组的8倍,并且对于长列表来说, get(int)的性能将非常慢……假设您以明显的方式使用它。

If you want something that can efficiently use as much memory as you can configure your JVM to use, then you should use an int[][] ie an array of arrays of integers, with the int[] instances being as large as you can make them. 如果您想要某种可以有效使用尽可能多的内存(可以配置JVM来使用)的内存,则应使用int[][]即整数数组的数组,并且int[]实例应尽可能大使他们。


I need to find Factor numbers from 2 to 2^63-1 using Sieve and sieve must have information that P[n]= is smallest prime with divide n. 我需要使用Sieve查找从2到2 ^ 63-1的因数,并且sieve必须具有P [n] =是除以n的最小素数的信息。 I know that with sieve i can Factorise number to 2^52. 我知道用筛子可以将因子分解为2 ^ 52。 But how do that exercises with holding on to the content. 但是,如何坚持内容呢?

I'm not really sure I understand you. 我不确定我是否了解您。 To factorize a number in the region of 2^64, you only need prime numbers up to 2^32 ... not 2^52. 要分解2 ^ 64范围内的数字,您只需要质数最大为2 ^ 32 ...而不是2 ^ 52。 (The square root of 2^64 is 2^32 and a non-prime number must have a prime factor that is less than or equal to its square root.) (2 ^ 64的平方根为2 ^ 32,并且非素数必须具有小于或等于其平方根的素数。)

It sounds like you are trying to sieve more numbers than you need to. 听起来您正在尝试筛除所需的更多数字。

If you really need to store that much data in memory, try using java.util.LinkedList collection instead. 如果确实需要在内存中存储那么多数据,请尝试使用java.util.LinkedList集合。

However, there's a fundamental flaw in your algorithm if you need to store 16GB of data in memory. 但是,如果您需要在内存中存储16GB数据,则算法中存在一个基本缺陷。 If you're talking about Sieve of Eratosthenes and you need to store all primes < 2^32 in an array, you still wouldn't need an array of size 2^32. 如果您正在谈论Eratosthenes的筛网,并且您需要将所有<2 ^ 32的素数存储在一个数组中,那么您仍然不需要大小为2 ^ 32的数组。 I'd suggest you use java.util.BitSet to find the primes and either iterate and print or store them in a LinkedList as required. 我建议您使用java.util.BitSet查找素数,然后根据需要迭代并打印或将其存储在LinkedList中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM