ok, so I am writing psuedo code trying to recreate the AKS test. One issue I'm running into is that I want to be able to do it with doubles and long doubles if possible. Its listed to have 6 major steps:
fist it has to be > 1
1 2 3 4
|
if (input<1)
{
throw error;
}
|
also it needs to be a whole number
1 2 3 4 5 6 7
|
#include<cmath>
double fractpart, intpart;
fractpart = modf (input, &intpart);
if (fractpart!=0)
{
throw error;
}
|
now the fist step is to pick to arbitrary whole values and see if the input value is equal to the one of the values raised to another. I will use 2 and 3 as my values (a & b) respectively. Now considering I am using floating point arithmetic I will have to have my own comparisons. Now please correct me if you think these methods are very wrong.
First: I compare them using an epsilon difference, then I compare their strings equivalents.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <sstream>
#include<string>
bool comparedoubles (double param1,double param2)
{
bool returnbool = false;
double copyparam1=param1;
double epsilon1 = abs(param1/cpoyparam1);
copyparam1=param2;
epsilon1+=abs(param2/copyparam1);
if (abs(param1-param2)<=epsilon1)
{
returnbool=true;
}
std::wostringstream strs1,strs2;
strs1 << param1;
std::wstring str1 = strs.str();
strs2 << param1;
std::wstring str2 = strs.str();
if (str1==str2)
{
returnbool=true;
}
return returnbool;
}
|
now that I have my comparison I will try to compare my input vs a^b.
1 2 3 4
|
if (comparedoubles(input,pow(2,3))
{
return false;
}
|
the next step is to find the smallest r such that or(n) > log2(n).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
double mod=0,base=0,notused,i=1;
bool done=false;
while (done==false)
{
while (!comparedoubles(base,1))
{
base = modf(pow(input,i),notused);
i++;
}
if (i>(log(input)/log(2)))
{
done=true;
}
else
{
base=0;
i++;
}
}
return i;
|
now the third step is If 1 < gcd(a,n) < n for some a ≤ r, output composite.
assume I have a gcd function already so:
1 2 3 4 5 6 7 8 9 10
|
//since a is already 2 in my function just return it as true
if (input==2)
{
return true;
}
//now to the function
if (1<gcd(2,input)&&gcd(input,2)<n)
{
return false;
}
|
now to step 4: If n ≤ r, output prime. This is pretty straight forward. I am going to refer to the i I generated earlier for r.
1 2 3 4
|
if (!(n<=i/*from earlier*/))
{
return false;
}
|
now this is where I need a little help, from step 5 For a = 1 to do
if (X+a)n≠ Xn+a (mod Xr − 1,n), output composite;
any ideas?