1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <stdio.h> #include <stdlib.h> #include<math.h> #include<malloc.h> typedef struct link { int num; struct link *next; } link;
void Initlink(link * first) { first->num=666; first->next=malloc(sizeof(link)); link * t=first->next; t->num=2; t->next=malloc(sizeof(link)); t=t->next;
t->num=3; t->next=malloc(sizeof(link)); t=t->next;
t->num=5; t->next=malloc(sizeof(link)); t=t->next;
t->num=7; t->next=malloc(sizeof(link)); t=t->next; t->next=NULL; }
void print_all(link * head) { head=head->next; while(head->next) { printf("%d\n",head->num); head=head->next;
} }
int main()//求个10^8内素数玩玩 { link * head=malloc(sizeof(link)); Initlink(head); int linknum=4; int min=10; for(int i=1; i<4; i++) { //printf("%d\n",panju); link * t; for(int j=min; j<min*min; j++) { t=head; int flag=0; t=t->next; for(int h=0;t->num<=sqrt(j)+1;h++) { if(j%t->num==0) { flag++; break; } t=t->next; } if(flag==0) { while(t->next) { t=t->next; } t->next=malloc(sizeof(link)); t->num=j; t=t->next; t->next=NULL; linknum++; } if(j==100000)printf("%d",linknum); } min=min*min; } //print_all(head); printf("%d",linknum); //printf("finish"); }
|