PRIME1 wrong answer. Passing own tests. Stuck.

I’ve been pulling my hair on the PRIME1 problem for some time. I seemingly take the correct input and output the correct answers when I do my own testing.

Would really appreciate a second pair of eyes here.

http://www.codechef.com/viewsolution/1893863

Ps. I know global variables are bad and that I can quickly generate the hardcoded primes using sieve of Eratosthenes.

Thanks,
NN

Hello,

I believe there are some issues with your code, although I’m also not sure about some of them,but:

  1. You don’t need to pass any arguments to main() and it should return 0…

  2. Since you’re declaring such a huge array as a global variable you can use static keyword as well, just to be safe :slight_smile:

  3. More importantly, you shouldn’t print any blank lines from within your program, so, this line:

    // formatting
    if (i > 0) {
    printf("\n");
    }

can, AND SHOULD, be removed.

Also, I’m really not sure if such a huge array is allowed in your program, so you should be careful with it…

Best regards,

Bruno

Hi, thanks for your answer.

  1. That won’t make any difference. It does return 0 (EXIT_SUCCESS).
  2. Tried but this made no difference.
  3. If I don’t print newlines, how will I get newlines in between my test cases in output?

Any other suggestions?

Thanks

Hey there.
I think its printing format and return EXIT_SUCCESS all are fine…instead
Your code isn’t working on bigger i/p’s

Try
1
100000000 100000100

it should print

100000007

100000037

100000039

100000049

100000073

100000081

So now I think you can figure out the rest…
Also nxt tym if you have any trouble plz. explain your approach in brief…it helps others to find the bug fast.

Happy Coding. :slight_smile:

1 Like

Regarding point number 2) by @kuruma

Adding static to a global variable does not make any difference. All global variables in C are static by default (and hence, will be auto-initialized to zero).

Also, all local variables of any function in c are having the storage auto as default. (These are not initialized to a particular value, and hence contain garbage).

Hi ! ,
Your approach doesn’t seem to give correct results for high values of the input. Why don’t you try the Sieve of Eratosthenes and modify it to suit the requirements of the question. I guess it’s a better approach which will fetch you correct results always as you can’t remember all those prime nos. you have in your code.

Thanks! Thanks to your comment I realized that if I started my “m” on an even number, I’d never generate any primes since I do += 2 :slight_smile: Silly me.

Never mind…we all do silly mistakes smtyms…That’s how we learn :slight_smile: