#include <iostream.h>
void prime_num(int num,int cut);
void center_list(int primes[],int len, int c,int n);
void main()
{
int N,C;
cin>>N>>C;
while(N>0)
{
prime_num(N,C);
cin>>N>>C;
}
}
void prime_num( int num,int cut)
{
int prime_list[169],k=0,i;
bool isPrime=true;
for(i=1;i<169;i++)
{
prime_list[i]=0;
}
for ( i = 1; i <= num; i++)
{
for ( int j = 2; j <i; j++)
{
if ( i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
prime_list[k]=i;
k++;
}
isPrime=true;
}
center_list(prime_list,k,cut,num);
}
void center_list(int primes[169],int len,int c,int n)
{
int pos,cut_len,i;
if(len%2==0)
{
cut_len=c*2;
}
else
{
cut_len=c*2-1;
}
pos=(len-cut_len)/2;
cout<<n<<" "<<c<<":";
if(cut_len<=len)
{
for (i = 0; i < cut_len; i++)
{
cout<<" "<<primes[pos+i];
}
cout<<"\n\n";
}
else
{
for (i = 0; i < len; i++)
{
cout<<" "<<primes[pos+i];
}
cout<<"\n\n";
}
}
Hello everyone! I have written the above code. The purpose is tofindthe prime numbers of a givennumber (N) put them in a list . Then cut the center list of length C*2 or C*2-1 and print it. Everything works ok but I need to make my programm faster. Any ideasplease??????