Use of long long in the loop

sir/ma’am

If I don’t use long long int in the loop with variable i it is not giving correct answer.(here b = 1000000)
here i is int and it can take og b = 1000000
why is this so .

    for(int i = 3 ; i <=b ; i+=2){
        if(prime[i] == 1){
            for(long long int j = i*i ; j <= b ; j = j + i){
                prime[j] = false;
            }
        }
    }

please reply

hi @sinhadivyam19, here i take value from 3 to b where b=10^6 , let’s take the case when i=b;
so j= i * i, i.e j=10^6 * 10^6 =10^12 which is greater than value supported by int,
Now you might think that you have added the condition j<=b then it should not exceed b, but what happens is first j takes the value i*i and then checks if it is less than b or not

In case of any doubt feel free to ask :slight_smile:
Mark your doubt as RESOLVED if you got the answer