[2,2^2=4,2^3=8,16,3,9,5,7,11,13,17,23]
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define UPPER_LIMIT 42
#define B 5
typedef struct primes_ {
int no;
struct primes_ * next;
} PrimesList;
int main (){
int i,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;
PrimesList *listS, *tempS;
listS=(PrimesList*)malloc(sizeof(PrimesList));
tempS=listS;
tempS->no=1;
tempS->next=(PrimesList*)malloc(sizeof(PrimesList));
tempS=tempS->next;
tempS->next=NULL;
while (temp->next !=NULL) {
int p=temp->no;
int k=0,flagS=0;
while (flagS==0) {
int z=pow(p,2);
if ((pow(z,k)) <= (UPPER_LIMIT)) {
tempS->no=pow(z,k);
tempS->next=(PrimesList*)malloc(sizeof(PrimesList));
tempS=tempS->next;
tempS->next=NULL;
}
else flagS=1;
k=k+1;
}
temp=temp->next;
}
temp=list;
tempS=listS;
while (tempS != NULL) {
printf("%d next: \n ",tempS->no);
tempS = tempS->next;
}
system("pause");
//getchar();
return 0;
}