-
Notifications
You must be signed in to change notification settings - Fork 32
/
GreatestPrimeNumberHaunting.c
150 lines (120 loc) · 4.02 KB
/
GreatestPrimeNumberHaunting.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Name: Minhas Kamal
* Occupation: Student (DU,IIT)
* Date: May.13
* Comment: I have tried to make this program more time efficient, but at the same
* time more complex. It finds all prime number in its domain (3 - 18,446,744,073,709,551,616).
**/
#include <stdio.h>
#include <math.h>
#include <time.h>
int main ()
{
clock_t st, fn;
long long int s,f; //starting & finishing number
printf ("Enter your starting number: ");
scanf ("%lld", &s);
printf ("Enter your finishing number: ");
scanf ("%lld", &f);
st=clock();
if (s>f)
{ //Starting number is smaller than finishing number
long long int temp = s;
s=f;
f=temp;
}
if (s<2)
{
s=2;
}
if(f<3)
{
f=3;
}
else
{
printf("\n\n##Prime numbers are:\n\n");
if(s<3){
printf("2,\t");
}
long long int x, w;
if(s%2==0) w=(s+1); //I have made starting number odd, this will save time
else w=s;
for(x=w; x<=f; x=x+2) //The program will work only with odd numbers
{
int z=1; //Here z is the flag
long long int y;
if(x>500)
{
if(x%3 == 0) z=0;
else if(x%5 == 0) z=0;
else if(x%7 == 0) z=0;
else if(x%11 == 0) z=0;
else if(x%13 == 0) z=0;
else if(x%17 == 0) z=0;
else if(x%19 == 0) z=0;
else if(x%23 == 0) z=0;
else if(x%29 == 0) z=0;
else if(x%31 == 0) z=0;
else if(x%37 == 0) z=0;
else if(x%41 == 0) z=0;
else if(x%43 == 0) z=0;
else if(x%47 == 0) z=0;
else if(x%53 == 0) z=0;
else if(x%59 == 0) z=0;
else if(x%61 == 0) z=0;
else if(x%67 == 0) z=0;
else if(x%71 == 0) z=0;
else if(x%73 == 0) z=0;
else if(x%79 == 0) z=0;
else if(x%83 == 0) z=0;
else if(x%89 == 0) z=0;
else if(x%97 == 0) z=0;
else if(x%101 == 0) z=0;
else if(x%103 == 0) z=0;
else if(x%107 == 0) z=0;
else if(x%109 == 0) z=0;
else if(x%113 == 0) z=0;
else if(x%127 == 0) z=0;
else if(x%131 == 0) z=0;
else if(x%137 == 0) z=0;
else if(x%139 == 0) z=0;
else if(x%149 == 0) z=0;
else if(x%151 == 0) z=0;
else if(x%157 == 0) z=0;
else if(x%163 == 0) z=0;
else if(x%167 == 0) z=0;
else if(x%173 == 0) z=0;
else if(x%179 == 0) z=0;
else if(x%181 == 0) z=0;
else if(x%191 == 0) z=0;
else if(x%193 == 0) z=0;
else if(x%197 == 0) z=0;
else if(x%199 == 0) z=0;
else if(x%211 == 0) z=0;
else if(x%223 == 0) z=0;
else if(x%227 == 0) z=0;
else if(x%229 == 0) z=0;
else if(x%233 == 0) z=0;
else if(x%239 == 0) z=0;
else if(x%241 == 0) z=0;
else if(x%251 == 0) z=0;
else if(x%257 == 0) z=0;
else for (y=263; y<=sqrt(x); y=y+2)
{
if(x%y == 0) {z=0; break;}
}
}
else for(y=3; y<=x/3; y=y+2) {if(x%y == 0) z=0;}
if(x<s || x<0){printf ("\n\aUnable to do more!\a\n\n"); return 0;}
else if (z==1) printf("%lld,\t",x);
}
printf("\n\n**END**\n");
}
fn=clock();
printf("\n\n**TIME: %d.%d sec \n\n", (fn-st)/1000, (fn-st)%1000);
//pause
char c;
scanf("%c%c", &c, &c);
return 0;
}