n0oO0o0Ob's blog

By n0oO0o0Ob, history, 8 days ago, In English

Getting TLE in this problem. Code:

#include <bits/stdc++.h>

using namespace std;
#define endl "\n"

int main()
{

    ios::sync_with_stdio(false);cin.tie(0);
    int a;
    cin >> a;
    for(int i = 1; i <= a; i++){
        if(a % i == 0){
            cout << i << endl;
        }
    }
    return 0;
}


 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
8 days ago, # |
Rev. 6   Vote: I like it 0 Vote: I do not like it

Just iterate from 1 to sqrt(n). And check if idivides n evenly or not. If idivides n evenly insert i and n/i in a set And then print all the elements of the set in newline.

»
8 days ago, # |
  Vote: I like it 0 Vote: I do not like it

this is not a efficient way of finding the factors of large numbers just iterate till the sqrt(n) and push (i and n/i+ if they are not equal else if equal push anyone among (i or n/i)

hope that helps