[求助]素数问题,他是如何求出最大公约数的
为什么这个程序不能输出1000000(一百万)以内的素数?#include<stdio.h>
#include<math.h>
struct node
{
int a;
struct node *p;
}typedef Node;
Node *Create()
{
Node *Pb;
Pb = new Node;
Pb->p = NULL;
return Pb;
}
Node *Insert(Node *Pa,int n)
{
Node *Pb;
Pb = new Node;
Pb->a = n;
Pa->p = Pb;
Pb->p = NULL;
return Pb;
}
void Output(Node *Pa)
{
Pa = Pa->p;
while(Pa)
{
printf("%d ",Pa->a);
Pa = Pa->p;
}
printf("\n");
}
void Destroy(Node *Pa)
{
Node *Pb;
while(Pa)
{
Pb = Pa->p;
delete Pa;
Pa = Pb;
}
}
void main()
{
int Ma,i,Ni=1;
Node *qa,*head,*qc;
head = Create();
qa = Insert(head,2);
qc = qa;
printf("Please input the number!\n");
scanf("%d",&Ma);
for(i=3;i<=Ma;i=i+2)
{ Node *qb;
qb = qc;
while(qb)
{
if(i%qb->a==0)
break;
qb = qb->p;
}
if(!qb)
{ qa = Insert(qa,i);
Ni++;
}
}
Output(head);
printf("%d\n",Ni);
Destroy(head);
}
[此贴子已经被作者于2006-12-25 13:42:54编辑过]