dimanche 3 février 2019

How can iI reduce time complexity of the following block of code?

I am taking 1 to n digits and finding count of numbers that are divisible by a or b but not divisible by both. I want to reduce time complexity of this block by some logical change.

cin >> n >> a >> b >> k;      
for(int i = 1; i <= n; i++) {
    if(i % a == 0 && i % b==0) {
        count++;
    } else if(i % b == 0 && i % a != 0) {
        count++;
    }
}

Aucun commentaire:

Enregistrer un commentaire