samedi 3 janvier 2015

Lucky Numbers Program not showing right answer

I'm making a lucky numbers program using normal arrays so that the rest of the numbers in the list doesn't move forward. The pattern I'm following is 1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, 87, 93, 99,... For more info: Lucky Numbers


Here's the program I've made:



public class LuckyNumbers {

public static void main(String[] args) {
int[] lucky = new int[101];

for (int a = 0; a < lucky.length; a++){
lucky[a] = a;
}
//goes through each element in list
for (int b = 2; b < lucky.length; b++){
//checks if number is surviving
if (lucky[b] != 0){
/* if it does survive, go through the list deleting elements
* (setting them to zero) that fall on the
* index of the multiples of the the surviving number*/
int luckyNum = lucky[b]; // so that the number doesn't change
for (int c = 1; c < lucky.length;c++){
int d = luckyNum * c;
if (d < lucky.length){
lucky[d] = 0;
continue;
}
}
}
}

for (int f = 0; f < lucky.length; f++){
if (lucky[f] != 0){
System.out.println(lucky[f]);
}
}
}
}


The output is 1. I think it is a logic error.


Aucun commentaire:

Enregistrer un commentaire