Sorry for keeping you busy by asking so many questions. I`m really trying to do it on my own...but it doesn't want to work.
I've upgraded the program after reading your advices. Right now there are two possible main loops, so the program has to analase only odd numbers. I've moved the switch with {2,3,5,7} further, so it's after the main loop. It won't be checked more than 10 times, I suppose it's not a problem anymore. Moreover, I've added quite a few "if's" to the printing loop-it checks whether the first number is even or odd, and whether it's not a 0,1 or 2(as these were troublesome).
The trouble is: none of these seem to work. I'm checking the program by checking the numbers from 1 to 10^7, it's still the same. I've checked the times of pure computations and pure printing by removing the main loop, then I've tried to mix the current version with the previous one. It seems that these changes just give around the same results in any combination.
I understand your post and what were the problems with previous version. I can see no mistake in my ideas nor in the implementations. Could you explain me what's wrong? Again, sorry for being so learn-proof; ). I sincerely hope it's not laziness-I've been checking it all day. Before your today's post I've thought of some things similar to your advices, but, again, changes weren't improving anything(In example, I've tried just to delete the {2,3,5,7} switch. Or eliminate the %2 in every iteration. The problem was the same as now-no changes in speed). If I should try harder before asking, say it; )
#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
unsigned int t;
unsigned int x;
scanf("%u", &t);
unsigned int * m=new unsigned int[t*2]; //Table m saves the borders of the interval
for(int j=0;j<t*2;j++)
scanf("%d", &m[j]); //Borders are inserted
for(unsigned int j=0;j<t;j++)
{cout<<endl;
x=1+m[1+2*j]-m[2*j]; //Var x remembers how many numbers are there to be analysed
bool * p = new bool[x]; //Table x remembers whether a number is prime or not(true stands for prime)
for(unsigned int u=0;u<x;u++)
{p[u]=false; //At the beggining all numbers are not considered to be prime
}
if(m[2*j]%2!=0)
for(unsigned int k=m[2*j];k<=m[1+2*j];k+=2) //In this loop numbers are analysed
{
{
{
for(unsigned int l=3;l*l<=k;l=l+2) //I find numbers which aren't prime using the Sieve of Erastotenes
{if (k%l==0)
{
p[k-m[2*j]]=false;
break;
}
p[k-m[2*j]]=true;
}
}
}
}
else
for(unsigned int k=m[2*j]+1;k<=m[1+2*j];k+=2) //In this loop numbers are analysed
{
{
{
for(unsigned int l=3;l*l<=k;l=l+2) //I find numbers which aren't prime using the Sieve of Erastotenes
{if (k%l==0)
{
p[k-m[2*j]]=false;
break;
}
p[k-m[2*j]]=true;
}
}
}
}
if(m[2*j]<8)
{
for(int o=0;o<8;o++)
switch(m[2*j]+o)
{case 2:
p[o]=true;
break;
case 3:
p[o]=true;
break;
case 5:
p[o]=true;
break;
case 7:
p[o]=true;
break;}
}
if(m[2*j]==2 or m[2*j]==0)
{ cout<<2<<endl;
for(unsigned int k=m[2*j]+1;k<=m[1+2*j];k+=2) //And cout every prime number from the given interval
{
if(p[k-m[2*j]]==true)
printf("%u\n", k);
}
}
else
if(m[2*j]%2==0)
for(unsigned int k=m[2*j]+1;k<=m[1+2*j];k+=2) //And cout every prime number from the given interval
{
if(p[k-m[2*j]]==true)
printf("%u\n", k);
}
else
if(m[2*j]==1)
{
cout<<2<<endl;
for(unsigned int k=m[2*j];k<=m[1+2*j];k+=2) //And cout every prime number from the given interval
{
if(p[k-m[2*j]]==true)
printf("%u\n", k);
}
}
else
for(unsigned int k=m[2*j];k<=m[1+2*j];k+=2) //And cout every prime number from the given interval
{
if(p[k-m[2*j]]==true)
printf("%u\n", k);
}
}
return 0;
}