Code: Select all
#include <stdio.h>
#include <stdlib.h>
typedef struct primes_ {
int no;
struct primes_ * next;
} PrimesList;
void main (){
int i, B = 1024, count, flag1;
PrimesList *list, *temp, *initial;
list = (PrimesList*)malloc(sizeof(PrimesList));
list->no = 2;
list->next = NULL;
temp = list;
count = 1;
i = 3;
while (i <= B) {
initial = list;
flag1 = 0;
while ( ((initial->next) != NULL) && (flag1 == 0) ) {
if ((i % initial->no) == 0){
flag1 = 1; // prime factor exists -> skip this number
}
else{
initial = initial->next; // advance to next prime
}
}
if (flag1 == 0) {
temp->next = (PrimesList*)malloc(sizeof(PrimesList));
temp = temp->next;
temp->no = i;
temp->next = NULL;
count++;
}
i += 2;
}
temp = list;
while (temp != NULL) {
printf("%d next: \n ",temp->no);
temp = temp->next;
}
printf("\ntotal: %d\n",count);
system("pause");
//getchar();
}