Блог пользователя The-Madara-uchiha

Автор The-Madara-uchiha, история, 7 месяцев назад, По-английски

// 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

Полный текст и комментарии »

  • Проголосовать: нравится
  • -37
  • Проголосовать: не нравится