File tree 2 files changed +76
-0
lines changed
2 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ .vs /cs-algorithms /v16 /Browse.VC.db
3
+ .vs /cs-algorithms /v16 /Browse.VC.opendb
4
+ .vs /ProjectSettings.json
5
+ .vs /slnx.sqlite
6
+ .vs /cs-algorithms /v16 /ipch /AutoPCH /cf84f1c192b8b4da /SIEVEOFERATOSTHENES.ipch
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string>
3
+ using namespace std ;
4
+ /*
5
+ Sieve of Eratosthenes
6
+
7
+ Compiled Using Visual Studio 2019
8
+ by Divakar Lakhera
9
+ */
10
+
11
+ /*
12
+ char* sieveOfEratosthenes(int limit)
13
+ - returns char* buffer of size "limit+1" with all the positions marked "1" for primes.
14
+ - returns NULL if "limit" is set to "0".
15
+ */
16
+
17
+ char * sieveOfEratosthenes (int limit)
18
+ {
19
+ if (limit != 0 ) {
20
+ char * buf = (char *)malloc (sizeof (char ) * (limit + 1 ));
21
+ if (buf == NULL )
22
+ {
23
+ cout << " Fatal: Internal error \" Cannot Malloc\" " << endl;
24
+ exit (-1 );
25
+ }
26
+ memset (buf, 1 , limit + 1 );
27
+
28
+ for (int i = 2 ; i * i <= limit; i++)
29
+ {
30
+ if (buf[i] == 1 )
31
+ {
32
+ for (int j = i * i; j <= limit; j += i)
33
+ {
34
+ buf[j] = 0 ;
35
+ }
36
+ }
37
+ }
38
+ return buf;
39
+ }
40
+ else
41
+ {
42
+ return NULL ;
43
+ }
44
+ }
45
+
46
+
47
+ void dispPrimes (int limit)
48
+ {
49
+
50
+ char * a = sieveOfEratosthenes (limit);
51
+ if (a != NULL ) {
52
+ for (int i = 2 ; i < limit; i++)
53
+ {
54
+ if (a[i] == 1 )
55
+ {
56
+ cout << i << " " ;
57
+ }
58
+ }
59
+ }
60
+ else
61
+ {
62
+ cout << " ERROR: Limit Set to 0" << endl;
63
+ }
64
+ }
65
+
66
+ int main ()
67
+ {
68
+ dispPrimes (100 );
69
+ return 0 ;
70
+ }
You can’t perform that action at this time.
0 commit comments