-
Notifications
You must be signed in to change notification settings - Fork 32
/
GreatPrimeNumberHaunting.c
74 lines (56 loc) · 1.83 KB
/
GreatPrimeNumberHaunting.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
/**
* Name: Minhas Kamal
* Occupation: Student (DU,IIT)
* Date: 16.May.13
**/
#include <stdio.h>
#include <math.h>
#include <time.h>
int main ()
{
clock_t start, finish;
unsigned long long int s, f; //starting & finishing number
printf ("Enter your starting number: ");
scanf ("%lld", &s);
printf ("Enter your finishing number: ");
scanf ("%lld", &f);
start = clock();
if (s>f) printf("Wrong information!\a\nstarting number is bigger.\a\n"); //starting number should be smaller than finishing number
else if (s<1 || f<2) printf ("\aError!\a"); //I have done this to save more time
else
{
if(s%2==0) s=(s+1); //I have made starting number odd, this will save time
unsigned long long int x; //holds & increases starting num's value
long long int y, y2; //y is the factorial of x & y2 contains square root of x
char a=0; //to arrange lines
char z; //here z is the flag
printf("\nThe first prime is: 2\nYour requirements are given bellow:\n\n");
for(x=s; x<=f; x=x+2) //the program will work only with odd numbers
{
y2=sqrt(x);
z=1;
for (y=3; y<=y2; y=y+2) //verification loop
{
if(x%y==0)
{
z=0;
break;
}
}
if (z==1) //printing
{
printf("%lld,\t",x);
a++;
if(a%10==0)
{
printf("\n");
a=0;
}
}
}
printf("\n\n");
}
finish = clock();
printf("TIME: %d.%.3d sec \n\n", (finish-start)/1000, (finish-start)%1000);
return 0;
}