# explain the #define in the following code why we are using it in the program (what is the purpose of using it)

#include <cstdio>

const int MAX = 10000;
const int LMT = 100;

int flag[157], cnt, primes[1230];

#define ifc(x) (flag[x>>6]&(1<<((x>>1)&31)))

#define isc(x) (flag[x>>6]|=(1<<((x>>1)&31)))

void sieve()

{

``````int i, j, k;
primes[0] = 2, cnt = 1;
for(i = 3; i < LMT; i+=2) if(!ifc(i)) for(j = i*i, k = i << 1; j < MAX; j += k) isc(j);
for(i = 3; i < MAX; i += 2) if(!ifc(i)) primes[cnt++] = i;
``````

}

int main()

{

``````int n, i, x, pwr, flg;
sieve();
while(scanf("%d", &n) == 1) {
for(i = flg = 0; i < cnt && primes[i] <= n; i++) {
x = n, pwr = 0;
while(x > 0) {
pwr += x / primes[i];
x /= primes[i];
}
if(pwr > 0) {
if(flg) printf(" * ");
printf("%d^%d", primes[i], pwr);
flg++;
}
}
printf("\n");
}
return 0;
``````

}

It works like a replacement, as instead of writing (flag[x>>6]&(1<<((x>>1)&31))) you can just pass value x in ifc(x) and get the result.

In simple Assume this #define code

``` # define read(n) scanf("%d",&n)
```

if you add this line above your code then while taking input you dont have to take input using scanf() you can just initialize variable say t and for taking input you can just code

``` read(t); and this will work as scanf("%d",&t);
```

Or, assume this one

``` # define ll long long int
```

If you add this link at the top of your code, then you donâ€™t have to initialize any long long int variable in code as

``` long long int a,b,...,x;
ll a,b,....,x;
```

Here ll can be used in replacement of long long intâ€¦

Hope you understoodâ€¦

In cases such as
#define a(x) 2x
Therefore whenever you call a(x)
it behaves as a function and returns the value of 2
x therefore if you write a(5) then it will return 10
Similiary just treat the above given define instructions as function declaration.

Hope this helps.

this program demostrates segmented sieve , fast and memory efficient version of sieve ,
if we store whole array position to store one flag , it is inefficient ,
we are storing each 32 bit to store 1 flag which reduces memory complexity by a factor of 1/32 .
now you have used 2 macros
ifc will check if the bitflag is 1 OR 0 â€¦

isc will set specific bit position 1
any nth value is located at (n/32)th position
and (n%32)th bit from LSB
but since we are neglecting odd nth value flag changes to (n/64)th position .
and bit position is (n/2)%32 which is same as (n>>1)&31
mode with a power of 2 is same as ANDing with (same power of 2)-1

2 Likes

#define it is used to declaration like global variables in program.variables are declared one time but we used it many times and it reduce the line of code and simple we used it. And it called macros.

@rishabhprsd7 i know the use of #define i want to know what purpose is being achieved by using it in the solution

@ japoorv i know the use of #define i want to know what purpose is being achieved by using it in the solution

I am sorry i didnâ€™t get your pointâ€¦
You say that you know about #defineâ€¦! then what is the problem you are facing? Can you please elaborate?

okay okay i got you, do you want to know that why is these #define code being used in this code?

It just makes your code more readable and understandable. Whatâ€™s the role a function, instead of writing the same code again and again just pass a value to the function and get the desired result similarly define can be sought to be a small function

@rishabhprsd yes

@japoorv why did you downvote my answer inspite of it being correct ?? , may i know ?? this is wrong