You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ArrayIndexOutOfBoundsException in net.openhft.koloboke.collect.impl.hash.MutableSeparateKVLongLHashGO.toArray(MutableSeparateKVLongLHashGO.java:270)
#40
Closed
silverminken opened this issue
Apr 23, 2015
· 2 comments
Hi,
I'm currently working to solve Project Euler programming tasks and I've recently started using Koloboke. When writing a solution for problem 60, I've gotten a strange ArrayIndexOutOfBoundsException when calling toLongArray() on a HashLongSet.
Sorry for the hodgepodge of code below, but I wanted you to be able to copy/paste/run the code below without any edits.
The line below throws the exception:
long[] tertArr = secondarySet.toLongArray();
No comments on the code please. I wasn't done yet :P
importjava.math.BigInteger;
importjava.util.Arrays;
importjava.util.BitSet;
importnet.openhft.koloboke.collect.map.hash.HashLongObjMap;
importnet.openhft.koloboke.collect.map.hash.HashLongObjMaps;
importnet.openhft.koloboke.collect.set.hash.HashLongSet;
importnet.openhft.koloboke.collect.set.hash.HashLongSets;
publicclassEuler60 {
// The primes 3, 7, 109, and 673, are quite remarkable. By taking any two// primes and concatenating them in any order the result will always be prime.// For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of// these four primes, 792, represents the lowest sum for a set of four primes// with this property.//// Find the lowest sum for a set of five primes for which any two primes// concatenate to produce another prime.// Analysis:// 2 cannot be a part of the resulting set, since 2 is the only prime ending// in 2. Same for 5.// Use sets to intersect valid pairs until a set of five remainsprivatestaticfinallong[] primes = generatePrimesWithESieve(100000);
privatestaticHashLongSetprimeSet = HashLongSets.newUpdatableSet(primes);
privatestaticlongmaxSum = 100000;
privatestaticfinalintMAX_INDEX = Arrays.binarySearch(primes, BigInteger.valueOf(25000).nextProbablePrime().longValue());
privatestaticfinallongMAX_PRIME = primes[primes.length - 1];
privatestaticHashLongObjMap<HashLongSet> primePairMap = HashLongObjMaps.getDefaultFactory().<HashLongSet> newUpdatableMap(MAX_INDEX);
publicstaticvoidmain(String[] args) {
longtime = System.currentTimeMillis();
for (inti = 1; i < MAX_INDEX; i++) {
HashLongSetprimarySet = getPairSet(primes[i], i);
long[] secArr = primarySet.toLongArray();
Arrays.sort(secArr);
for (intj = 0; j < secArr.length; j++) {
if (primes[i] + secArr[j] * 4 > maxSum) break;
HashLongSetsecondarySet = HashLongSets.getDefaultFactory().newMutableSet(primarySet);
secondarySet.retainAll(getPairSet(secArr[j]));
if (secondarySet.size() < 3) continue;
long[] tertArr = secondarySet.toLongArray();
Arrays.sort(tertArr);
for (intk = 0; k < tertArr.length; k++) {
if (primes[i] + secArr[j] + tertArr[k] * 3 > maxSum) break;
HashLongSettertiarySet = HashLongSets.getDefaultFactory().newMutableSet(secondarySet);
if (tertiarySet.size() < 2) continue;
tertiarySet.retainAll(getPairSet(tertArr[k]));
long[] quatArr = tertiarySet.toLongArray();
Arrays.sort(quatArr);
for (intl = 0; l < quatArr.length; l++) {
if (primes[i] + secArr[j] + tertArr[k] + quatArr[l] * 2 > maxSum) break;
HashLongSetquaternarySet = HashLongSets.getDefaultFactory().newMutableSet(tertiarySet);
if (quaternarySet.size() < 1) continue;
quaternarySet.retainAll(getPairSet(quatArr[l]));
long[] quinArr = quaternarySet.toLongArray();
Arrays.sort(quinArr);
for (intm = 0; m < quinArr.length; m++) {
if (primes[i] + secArr[j] + tertArr[k] + quatArr[l] + quinArr[m] > maxSum) break;
System.out.format("{ %1$d, %2$d, %3$d, %4$d, %5$d } Sum = %6$d", primes[i], secArr[j], tertArr[k], quatArr[l], quinArr[m], primes[i]
+ secArr[j] + tertArr[k] + quatArr[l] + quinArr[m]);
System.out.println();
longsum = primes[i] + secArr[j] + tertArr[k] + quatArr[l] + quinArr[m];
if (sum < maxSum) {
maxSum = sum;
}
}
}
}
}
}
System.out.println("Result = " + maxSum);
System.out.println("Done.");
System.out.println("Solution took " + (System.currentTimeMillis() - time) + " ms.");
}
privatestaticHashLongSetgetPairSet(longprime) {
returngetPairSet(prime, Arrays.binarySearch(primes, prime));
}
privatestaticHashLongSetgetPairSet(longprime, intprimeIndex) {
HashLongSetret = primePairMap.get(prime);
if (ret == null) {
primePairMap.put(prime, (ret = getPairs(prime, primeIndex)));
}
returnret;
}
privatestaticHashLongSetgetPairs(longprime, intprimeIndex) {
HashLongSetpairSet = HashLongSets.newUpdatableSet();
longprimeDigitSum = digitSum(prime);
for (intj = primeIndex + 1; j < MAX_INDEX; j++) {
if ((primeDigitSum + digitSum(primes[j])) % 3 == 0) continue;
if (isPrime(concat(prime, primes[j])) && isPrime(concat(primes[j], prime))) pairSet.add(primes[j]);
}
returnpairSet;
}
privatestaticbooleanisPrime(longnum) {
if (num > MAX_PRIME) { returnBigInteger.valueOf(num).isProbablePrime(20); }
returnprimeSet.contains(num);
}
publicstaticlong[] generatePrimesWithESieve(longupperLimit) {
// No even numbers in sieve BitSet// BitSet starts from 0..sieveBound// p = 2i+3 => i = (p-3)/2// jStart = 3p// jStep = 2p// i = 0 => p = 3 => jStart = 9, jStep = 6// i = 0, j = 3, 6, 9// i = 1 => p = 5 => jStart = 15, jStep = 10// i = 1, j = 6, 11, 16// i = 2 => p = 7 => jStart = 21, jStep = 14// i = 2, j = 9, 16, 23intintUpperLimit = Math.toIntExact(upperLimit);
intsieveBound = (intUpperLimit - 1) / 2;
intupperSqrt = ((int) Math.sqrt(upperLimit) - 1) / 2;
BitSetset = newBitSet(sieveBound + 1);
set.set(0, sieveBound + 1);
for (inti = 0; i <= upperSqrt; i++) {
if (set.get(i)) {
for (intj = 3 * i + 3; j <= sieveBound; j += 2 * i + 3) {
set.clear(j);
}
}
}
inti = 0;
// Number of primes can be approximated with pi(x) = x/log(x - 1)LongArraylist = newLongArray((int) (upperLimit / (Math.log(upperLimit) - 1.08366)));
list.setSizeIncreaseFactor(1.2d);
list.add(2); // sieve starts at 3do {
list.add(2 * i + 3);
} while ((i = set.nextSetBit(i + 1)) != -1);
returnlist.getArray();
}
publicstaticlongconcat(longa, longb) {
for (longc = b; c > 0;) {
a *= 10;
c /= 10;
}
returna + b;
}
publicstaticlongdigitSum(longnum) {
longsum = 0;
while (num > 0) {
sum += num - ((num / 10) * 10);
num /= 10;
}
returnsum;
}
publicstaticclassLongArray {
privatestaticfinalintDEFAULT_SIZE = 16;
privatedoublesizeIncreaseFactor = 1.5d;
privatelong[] arr;
privateintsize = 0;
publicLongArray() {
arr = newlong[DEFAULT_SIZE];
}
publicLongArray(intinitialSize) {
arr = newlong[initialSize];
}
publicvoidadd(longvalue) {
if (size == arr.length) {
increaseSize();
}
arr[size++] = value;
}
publiclong[] getArray() {
trim();
returnarr;
}
publiclongget(inti) {
returnarr[i];
}
publicvoidtrim() {
if (size < arr.length) {
long[] newArr = newlong[size];
System.arraycopy(arr, 0, newArr, 0, size);
arr = newArr;
}
}
privatevoidincreaseSize() {
long[] newArr = newlong[(int) (arr.length * sizeIncreaseFactor)];
System.arraycopy(arr, 0, newArr, 0, arr.length);
arr = newArr;
}
publicdoublegetSizeIncreaseFactor() {
returnsizeIncreaseFactor;
}
publicvoidsetSizeIncreaseFactor(doublesizeIncreaseFactor) {
this.sizeIncreaseFactor = sizeIncreaseFactor;
}
}
}
The text was updated successfully, but these errors were encountered:
Hi,
I'm currently working to solve Project Euler programming tasks and I've recently started using Koloboke. When writing a solution for problem 60, I've gotten a strange ArrayIndexOutOfBoundsException when calling toLongArray() on a HashLongSet.
Sorry for the hodgepodge of code below, but I wanted you to be able to copy/paste/run the code below without any edits.
The line below throws the exception:
long[] tertArr = secondarySet.toLongArray();
No comments on the code please. I wasn't done yet :P
The text was updated successfully, but these errors were encountered: