Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

lucky numbers

807600Nov 11 2007 — edited Nov 14 2007
my code prints lucky numbers,
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99 etc
http://en.wikipedia.org/wiki/Lucky_number
but i don't like the eliminate method. instead of a boolean array,would using an arraylist be better, so i could remove the appropriate numbers?
class LuckyNumbers {

    static boolean bls[];

    public static void main(String[] args) {
	for(int n = 1; n <= 300; n++) {
	    if(isALuckyNumber(n)) System.out.print(n+" ");
	}
    }

    static boolean isALuckyNumber(int n) {
	bls = new boolean[n+1];
	for(int i = 2; i < bls.length; i++) {
	    if(bls[i] == false) eliminate(i);
	}

	if(bls[n] == false) return true;
	else return false;
    }

    static void eliminate(int n) {
	int count = 0;
	for(int i = 1; i < bls.length; i++) {
	    if(bls[i] == false) count++;
	    if(count == n) {
		bls[i] = true;
		count = 0;
	    }
	}
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2007
Added on Nov 11 2007
21 comments
555 views