The-Madara-uchiha's blog

By The-Madara-uchiha, history, 7 months ago, In English

// Sometimes, we need to find factors of a number to solve a problem. There are many ways to find factors of a number. Here, I am sharing a code :

vector<int> factors; // vector is a dynamic array , which size is not fix

for (int i = 1; i <= sqrt(a) + 1; ++i) {
    if (a % i == 0) {
        factors.push_back(i);
        if (i != a / i) factors.push_back(a / i);
    }
}



--> First, we are using a vector because we don't know the size.
-->Then, we are using a for loop up to the square root of that number.
--> Then, we are using two conditions to add factors. The first condition adds factors before the square root, and the second one adds factors after the square root.

Additionally, we can sort this vector as per our need..... Thank you

  • Vote: I like it
  • -37
  • Vote: I do not like it

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by The-Madara-uchiha (previous revision, new revision, compare).

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by The-Madara-uchiha (previous revision, new revision, compare).