-
Notifications
You must be signed in to change notification settings - Fork 13
/
prime genrator sol.cpp
122 lines (104 loc) · 2.59 KB
/
prime genrator sol.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* structure containing a data part and link part */
struct node
{
long data ;
struct node * link ;
} ;
void append ( struct node **, long ) ;
void display ( struct node * ) ;
int main( )
{
struct node *p ,*temp=NULL;
int i=0;
scanf("%d",&i);
while(i>0)
{
long k,q, kw=2, m=0;
if(k==3)
{
printf("n3n5n7");
k=11;
}
if(k>3 && k<=5)
{
printf("n5n7");
k=11;
}
if(k>5 && k<=7)
{
printf("n7");
k=11;
}
if(k>7 && k<10)
{
k=11;
}
while(k<=q)
{
int sq=(int)sqrt(k);
int b=1;
if((k%2==0 || k%5==0))
{
b=0;
}
else
{
temp=p;
while(temp!=NULL && temp->data<=sq)
{
if(k%temp->data==0)
{
b=0;
break;
}
temp=temp->link;
}
}
//printf(" nk = %ld have b=%d " ,k,b);
if(b)
printf("n%ld",k);
k++;
}
// display(p);
/* */ }
return 0;
}
/* adds a node at the end of a linked list */
void append ( struct node **q, long num )
{
struct node* temp;
struct node* r ;
if ( *q == NULL ) /* if the list is empty, create first node */
{
temp = (node*)malloc(sizeof(struct node));
temp -> data = num ;
temp -> link = NULL ;
*q = temp ;
}
else
{
temp = *q ;
/* go to last node */
while ( temp -> link != NULL )
temp = temp -> link ;
/* add node at the end */
r = (node*)malloc ( sizeof ( struct node ) ) ;
r -> data = num ;
r -> link = NULL ;
temp -> link = r ;
}
}
/* displays the contents of the linked list */
void display ( struct node *q )
{
/* traverse the entire linked list */
while ( q != NULL )
{
printf ( "n" ) ;
printf ( "%d ", q -> data ) ;
q = q -> link ;
}
}