Hello everyone. I saw a post of one of our friends here about a problem he found of the SPOJ website. So I went there to check it. I found it to be very cool; I started to do the exercises but when I got in the 2nd, a prime number generator, I got stuck. I did made the program and it works as it should, however, it's execution time is too long. So I'm coming here to ask for you to help me make my program more efficient. Here's my source code:
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_VALUE 1000000000
#define MAX_VALUE_FOR_N_AND_M 100000
fstream Arquivo("Arquivo.txt", ios::out);
int t = 0;
int m = 0;
int n = 0;
int p = 0;
bool Check_Sucessfull;
void Ask_Imputs(int&,int&,int);
void Check_Imput(int,int,bool&);
void Show_Primes(int,int, int);
void Ask_Imputs(int& m, int& n, int t)
{
cin >> t;
for (int a = 0; a < t; a++)
{
Check_Sucessfull = false;
m = 0; n = 0;
cin >> m >> n;
printf("\n");
Check_Imput(m,n,Check_Sucessfull);
if (Check_Sucessfull == true)
Show_Primes(m,n,p);
}
}
void Check_Imput(int m, int n, bool& Check_Sucessfull)
{
if (m >= 1 && m <= n && n <= MAX_VALUE && n-m <= MAX_VALUE_FOR_N_AND_M)
Check_Sucessfull = true;
else
Check_Sucessfull = false;
}
void Show_Primes(int m, int n, int p)
{
int Count;
for (p = m; p <= n; p++)
{
Count = 0;
if (p > 2 && p % 2 == 0)
continue;
else if (p > 3 && p % 3 == 0)
continue;
else if (p > 5 && p % 5 == 0)
continue;
if (p > 2)
{
for (int a = 1; a <= p; a++)
{
if (p % a == 0)
{
Count++;
if (Count > 2)
{
continue;
}
}
}
}
if (Count == 2 || p == 2)
Arquivo << p << endl;
}
Arquivo << "\n";
}
int main()
{
Ask_Imputs(m,n,t);
Arquivo.close();
return 0;
}
Thanks in advance, kind regards,
Petcheco