I’m solving a problem that consist in generate the first n<1000000 lucky numbers, but the problem is that it have to generete them in les than 2 seconds, I’ve tried different ways but I always have time limit exceeded, I was thinking also of changing this algorithm using Boolean but no results.
here you can find the sieve of lucky numbers as a reference http://en.wikipedia.org/wiki/Lucky_number
the lucky numbers sequence is the following
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99…
this is the code I created using ArrayList the inefficient one, if you have any hint that I can use to solve it, in time, I would appreciate it.
public class Luckyumbers {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
Integer max = 200000;
int b;
int c = 0;
int p;
for (int i = 1; i <= max; i += 2) {
numbers.add(i);
}
// System.out.println(numbers);
Integer bbb = 3;
Integer j = numbers.size();
int a = 3;
while (bbb < j) {
b = numbers.size();
p = 1;
for (int i = bbb; i < b; i += bbb) {
numbers.remove(i-p);
p = p + 1;
}
b = numbers.size() - 1;
c = numbers.get(b);
j = j - numbers.size() / bbb;
bbb = numbers.get(a-1);
a = a + 1;
// System.out.println(numbers);
}
for (int k = 0; k < numbers.size(); k++) {
int z = numbers.get(k);
System.out.print(z + " ");
}
}
}
Improved version I changed the code with the suggestions that everyone gave me and I reduced the time of the algorithm to 13 seconds for 1 million but stills to slow
public static void main(String[] args) {
int max= 1000000;
boolean[] numbers = new boolean[max];
for (int i = 2; i < numbers.length; i+=2)
numbers[i]=true;
int k=2,j=0,l=0,ant=0;
int p=0;
for (int i = 2; (k+k) < numbers.length; i++) {
k=which(numbers,i);
l=0;
p=0;
int sw=0;
boolean untrue=false;
for (j = l; l < numbers.length; j++) {
if((p==k)&&sw==1){
numbers[l]=true;
untrue=true;
p=0;
}
l=Next(numbers,l);
//if (sw ==1)
p++;
sw=1;
}
if (!untrue)
break;
}
System.out.println(counter(numbers));
}
static int which(boolean[] numbers,int i){
int k=0;
int l;
for (l = 0; l < i; l++) {
k=Next(numbers,k);
}
return k;
}
static int Next(boolean[] numbers,int i){
for (int l = i+1; l < numbers.length; l++) {
if (numbers[l]==false)
return l;
}
return numbers.length+1;
}
static int counter(boolean[] numbers){
int c=0;
for (int j = 1; j < numbers.length; j++)
if(numbers[j]==false)
c++;
return c;
}
}
It will (probably) be much faster to use an array of flags and set each element to a special value when eliminated from the sieve. That way you don’t need to create N Integer objects, add them to a collection, then remove them again.
The bit to be careful with will be determining the ‘sieve’ multiple for the next iteration…
Several other answers discuss the inefficiencies when removing elements from an
ArrayList.Note also that creating the objects in the first place takes time. Creating an
int[]of 10M elements and writing a value to each element takes 50ms on my system, but doing the same with an array ofIntegerobjects takes 1100ms, which is over half of your target time just for the set-up!Creating and populating an 10M-element
ArrayList<Integer>takes 1500ms, even when you pre-size it, and aLinkedList<Integer>takes 3200ms, so you’re out of time before you even start sieving.Update: having tried this (without the special casing suggested by btilly) it is indeed much faster, sieving 1M input numbers in 8.6s versus 32.5s for the original and 10M input numbers in 35s versus 137s for the original.
I also tried using a bit array rather than an
intarray, which obviously saves a lot of memory but was about half the speed.Another thought – there are a lot of questions on SO about prime number sieves, which may also discuss similar performance techniques?