Solovay-Stassen素性检验的C语言实现
我在这里用C语言实现了Solovay-Stassen概率素性检验,代码如下:
// Solovay-Stassen素性检验.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include"conio.h"
#include"stdlib.h"
#include"string.h"
#include"math.h"
typedef long long prime;
typedef int index;
typedef struct PN//构造素数节点
{
union
{
prime data;
long long length;
};
struct PN *next;
}PN;
typedef struct PF//构造质因数分解
{
prime data;
index power;
struct PF *next;
}PF;
typedef struct CRS//简化剩余系结构体
{
union
{
long long data;
int length;
};
struct CRS * next;
}CRS;
static PN* PNApplyNode(prime x)//申请新的素数结点
{
PN* s = (PN*)malloc(sizeof(PN));
if (s == NULL)
return NULL;
s->data = x;
s->next = NULL;
return s;
}
static PF* PFApplyNode(prime x, index P)//申请新的素数结点
{
PF* s = (PF*)malloc(sizeof(PF));
if (s == NULL)
return NULL;
s->data = x;
s->power = P;
s->next = NULL;
return s;
}
static CRS* ApplyNode(long long x)//申请简化剩余系新节点
{
CRS* s = (CRS*)malloc(sizeof(CRS));
if (s == NULL)
return NULL;
s->data = x;
s->next = NULL;
return s;
}
void PNListInitiate(PN *head)//素数列表初始化
{
head = (PN*)malloc(sizeof(PN));
head->next = NULL;
head->length = 0;
}
void PFListInitiate(PF *head)//质因数分解列表初始化
{
head = (PF*)malloc(sizeof(PF));
head->next = NULL;
}
void CRSInitiate(CRS* head)//简化剩余系头结点初始化
{
if (head == NULL)
exit(0);
head->next = NULL;
head->length = 0;
}
bool