I have a loop that goes from 3 to the phi of two prime user-inputted numbers (these numbers can be any length), finds all the prime numbers between the two, and stores them into an array.
My code for the loop:
int coPrimes(int num)
{
for (int i=3; i<=num; i++)
if(isPrime(i))
coprimes.push_back(i);
This loop takes a long period of time to finish.
Enter a prime number for 'a': 601
Enter a prime number for 'b': 769
a value: 601
b value: 769
product: 462169
phi: 460800
pubKey: 19
privKey: 145515
Process returned 0 (0x0) execution time : **756.670 s**
Press any key to continue.
I need this loop to work at a faster speed. Is there a more efficient method of performing this action?
P.S. My loop calls another function,isPrime
, here is the code for it if anyone wanted it.
bool isPrime(int num)
{
bool prime = true;
if(num < 2)
prime = true;
for (int i=2; i<num; i++)
if (num % i == 0)
prime = false;
return prime ;
}