0

We need to write a code for generating prime numbers in a given range.

Errors

  1. I have optimized the code many times but still getting time limit exceeded on spoj.com.
  2. My code exits with exit code 1

Here is my code:

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int num1, num2, i, j, flag, temp,two=2,test;

    scanf("%d",&test);
    for(int z=0;z<test;z++)
    {
        scanf("%d %d", &num1, &num2);
        if(num1<=2)
        {
            printf("%d\n",two);
        }
        temp = num1;
        if ( num1 % 2 == 0)
        {
            num1++;
        }
        for (i = num1; i <= num2; i = i + 2)
        {
            flag = 0;
            for (j = 3; j <= i / 2; j+=2)
            {
                if ((i % j) == 0)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0&&i!=1)
            {
                printf("%d\n", i);   
            }
        }
    }      
}

I have also attached images for the showing the errors.

Please help me optimizing the code or tell me what is wrong.

Chase
  • 5,315
  • 2
  • 15
  • 41

0 Answers0