I recently made a program that looks for quadratic equations (ax˛+bx+c = N) that, when incremented values of x are inputted, generate a lot of prime numbers. The problem is, when going for 10000 or 100000 different values for x, it has to check each result N for primality, which is slow. Plus, it checks for all combinations of a, b, and c, over a range, which REALLY slows it down.

Any suggestions for speeding up this program at all? Even a little bit will speed it up, because this is on the order of millions or billions of math calculations and loops.

Thanks.

Code:#include <iostream> #include <string> #include <cstdlib> #include <stdio.h> #include <math.h> #include <ctype.h> using namespace std; void main(){ short amin; short amax; short bmin; short bmax; short cmin; short cmax; short iterations; float pmin; float pmax; cout << "ax^2 + bx + c" << endl; cout << " " << endl; //RANGE FOR A cout << "Range for a? (integers -32,768 to 32,767)" << endl; cout << " " << endl; cout << "min? "; cin >> amin; cout << " " << endl; cout << "max? "; cin >> amax; cout << " " << endl; cout << " " << endl; //RANGE FOR B cout << "Range for b? (integers -32,768 to 32,767)" << endl; cout << " " << endl; cout << "min? "; cin >> bmin; cout << " " << endl; cout << "max? "; cin >> bmax; cout << " " << endl; cout << " " << endl; //RANGE FOR C cout << "Range for c? (integers -32,768 to 32,767)" << endl; cout << " " << endl; cout << "min? "; cin >> cmin; cout << " " << endl; cout << "max? "; cin >> cmax; cout << " " << endl; cout << " " << endl; //ITERATIONS cout << "How many iterations? (no more than 65,535)" << endl; cin >> iterations; cout << " " << endl; cout << " " << endl; //PERCENTAGE PRIME cout << "What percentage should be Prime? (integer 0 to 100)" << endl; cout << " " << endl; cout << "min? "; cin >> pmin; cout << " " << endl; cout << "max? "; cin >> pmax; cout << " " << endl; cout << " " << endl; cout << "Wait..." << endl; cout << " " << endl; cout << " " << endl; float percentmin = pmin/100; float percentmax = pmax/100; //START //Values for A for (short a = amin; a <= amax; a++) { if(a==0) //skips the linears { } else { //Values for B for (short b = bmin; b <= bmax; b++) //CHANGE BACK TO b++ { //Values for C for (short c = cmin; c <= cmax; c++) //CHANGE BACK TO c++ { //keeps track of how many primes result for this quadratic unsigned short primes = 0; //keeps track of the total iterations for this quadratic unsigned short total = 0; //START of test for increasing values of i for(short i=0;i<iterations;i++) { short flag = 0; __int64 n = ((a*i*i) + (b*i) + c); if(n>1) { if((n%2)==0) //checks against 2 { flag = 1; } else { // checks odds from 3 up to sqrt(n) for(long m = 3;m<=((long)(sqrt(n)));m+=2) { if((n%m)==0) { flag = 1; break; } } } if(flag==0) { primes = primes + 1; } total++; } else { /*cout << " " << endl; cout << "---------------------------------------" << endl; cout << n << " was tossed out for being <= 1..." << endl; cout << "---------------------------------------" << endl; cout << " " << endl;*/ } } double primepercent = ((double) primes) /(iterations); if((primepercent >= percentmin) && (primepercent <= percentmax)) { cout << " " << endl; cout << a << "x^2 + " << b << "x + " << c << endl; cout << primes << " out of " << total << " were prime." << endl; //cout << primes << endl; } } } } } }