I wrote this to find out which 10-digit numbers are prime but it stops after showing about 20 numbers at all. Would you help me figure out what's wrong? Or suggesting any other sources? It's important to me as it's my school project.
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void temp()
{
static long long n1=9999999999 ;
long long n2,n3;
if (n1 <= 1 || n1%2==0|| n1%3==0|| n1%5==0|| n1%7==0|| n1%11==0|| n1%13==0|| n1%17==0|| n1%19==0|| n1%23==0|| n1%29==0|| n1%31==0|| n1%37==0|| n1%41==0|| n1%43==0|| n1%47==0|| n1%53==0|| n1%59==0|| n1%61==0|| n1%67==0|| n1%71==0|| n1%73==0|| n1%79==0|| n1%83==0|| n1%89==0|| n1%97==0)
{
std::cout<< n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2 = (n1 - 1)/2;
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout<< n1 << " not prime\n\n";
}
else
{
n2--;
}
}
std::cout<< n1 << " prime\n\n";
n1--;
temp();
}
}
int main(int argc, char** argv)
{
temp();
return 0;
}
When n1 is 9999999983 it gets stuck inside the while loop until n2 is less or equal to 1
You should look for a more efficient way
while (n2 > 1)
{
n3 = n1 % n2;
if (n3 == 0)
{
std::cout << n1 << " not prime\n\n";
n1--;
temp();
}
else
{
n2--;
}
}
EDIT: This is what you could do in your case (Found it here and changed it to fit your needs (I found it here and changed it up a bit to fit your needs)
#include <iostream>
using namespace std;
void check() {
int n = 9999999999;
while (n != 0) {
int i, m = 0, flag = 0;
m = n / 2;
for (i = 2; i <= m; i++)
{
if (n % i == 0)
{
cout << "Number is not Prime." << endl;
flag = 1;
break;
}
}
if (flag == 0)
cout << "Number is Prime." << endl;
n--;
}
}
int main()
{
check();
return 0;
}