Found this problem on one of the sites.
The idea is to identify a series of numbers as prime based on their ordinal and flag them accordingly based on the following conditions :
a) If prime, concatenate its ordinal in the array to the output string.
b) If not prime, concatenate zero.
Here is the algorithm in C#.
string statstring = String.Empty;
bool ipr = false;
double[] primetest = { 2, 65537, 2999999, 4999999, 98320414332827, 2 ^ 35 - 1, 34567875111, 12345678987654321 };
for (int i = 0; i < 8; i++)
{
ipr = IsPrimeNumber(primetest[i]);
if (ipr == true)
{
statstring += (i + 1).ToString();
}
else
{
statstring += "0";
}
}
MessageBox.Show(statstring);
static bool IsPrimeNumber(double num)
{
bool bPrime = true;
double factor = num / 2;
int i = 0;
for (i = 2; i <= Math.Sqrt(num); i++)
{
if ((num % i) == 0)
{
bPrime = false;
break;
}
}
return bPrime;
}
IsPrimeNumber is a function that determines if a given number is prime or not. Got this on the net, the only improvement being that I added break condition in the loop to exit if prime. (reduces the number of iterations).
Comments