AKS算法,是三位印度人发明的,AKS是他们的姓氏首字母。ASK算法是确定算法,其时间复杂度相当于多项式的,属于可计算的算法。
另外需要了解的是Miller-Rabin素性测试算法。该算法不是确定算法。然而测试的计算速度快,比较有效,被广泛使用。
代码来自rosettacode.org的AKS test for primes。
C语言程序代码如下:
#include <stdio.h>
#include <stdlib.h>
long long c[100];
void coef(int n)
{
int i, j;
if (n < 0 || n > 63) abort(); // gracefully deal with range issue
for (c[i=0] = 1; i < n; c[0] = -c[0], i++)
for (c[1 + (j=i)] = 1; j > 0; j--)
c[j] = c[j-1] - c[j];
}
int is_prime(int n)
{
int i;
coef(n);
c[0] += 1, c[i=n] -= 1;
while (i-- && !(c[i] % n));
return i < 0;
}
void show(int n)
{
do printf("%+lldx^%d", c[n], n); while (n--);
}
int main(void)
{
int n;
for (n = 0; n < 10; n++) {
coef(n);
printf("(x-1)^%d = ", n);
show(n);
putchar('\n');
}
printf("\nprimes (never mind the 1):");
for (n = 1; n <= 63; n++)
if (is_prime(n))
printf(" %d", n);
putchar('\n');
return 0;
}